Sei sulla pagina 1di 17

Android Developer Fundamentals

Hello World

Lesson 1.3
Text and Scrolling Views

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 1
4.0 International License
Text and Scrolling Views

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 2
4.0 International License
TextView and
ScrollView

Android Developer Fundamentals 3


TextView for text
● TextView is a View component.
● Useful for displaying multi-line text.
● TextView is a complete text editor
○ EditText is a subclass of TextView that offers editing.
● Controlled with layout attributes.

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 4
4.0 International License
What about large amounts of text?
● The user may need to scroll.
○ Examples: news stories, articles, etc.
● If contents of TextView is larger than display, embed
TextView in a ScrollView component.
● Other Views can be embedded in a ScrollView.
○ TextView, Button, etc.

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 5
4.0 International License
ScrollView: for scrolling content
● Simple to use in the XML layout.
● Can scroll content on and off the screen.
● ScrollView is a FrameLayout, which is a ViewGroup.
● Can only hold one View (but that View can be a Layout).

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 6
4.0 International License
ScrollView considerations
● Easy, but not good for long lists.
● Uses too many resources.
● Don’t nest Scrollable Layouts.
● Consider using a RecyclerView for real-world apps.

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 7
4.0 International License
ScrollView: one child view (TextView)

<ScrollView...>
<TextView...> One child of ScrollView

</ScrollView>

Embedding the TextView in a ScrollView


makes the text of that TextView scrollable

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 8
4.0 International License
ScrollView: XML layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

… child view of the ScrollView [can only have 1 child] …

</ScrollView>

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 9
4.0 International License
ScrollView layout with one TextView
Example of a layout incorporating a ScrollView with one TextView:

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/article_subheading">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/article"
android:lineSpacingExtra="5sp"
android:autoLink="web"
android:text="@string/article_text"/>

</ScrollView>

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 10
4.0 International License
ScrollView
with a View
Group

Android Developer Fundamentals 11


ScrollView layout with a view group
<ScrollView
...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/article_subheading"
.../>

<TextView
android:id="@+id/article"
... />

</LinearLayout>
</ScrollView>

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 12
4.0 International License
ScrollView layout with multiple
elements
<ScrollView...>
<LinearLayout...> One child of ScrollView
But it can be a Layout
<ImageView.../>
<Button.../> Children of the Layout

<TextView.../>
</LinearLayout>
</ScrollView>
This work is licensed under a Creative
Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 13
4.0 International License
Working with
free-form text

Android Developer Fundamentals 14


Working with free-form text
If using a string resource:
● Use simple HTML tags for bold and italics.
● Text lines extend past margin. Each new
line represents an entire paragraph.
● Enter \n to r a line or paragraph.
● Escape apostrophes and quotes.
● Use only bold and italic HTML tags.
● Don’t use HTML tag for URLs.

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 15
4.0 International License
Active web links: autoLink attribute
Don’t use HTML for a web link. Just add the URL itself, and
the autoLink attribute set to "web":
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/article"
android:lineSpacingExtra="5sp"
android:autoLink="web" autoLink attribute
android:text="@string/article_text"/>

This work is licensed under a Creative


Commons Attribution-NonCommercial
Android Developer Fundamentals Text and Scrolling Views 16
4.0 International License
Android Developer Fundamentals Hello World - Creating Your First Android App 17

Potrebbero piacerti anche