How can you have multiple paragraphs in a ScrollView? XML
I am trying to simply put a few paragraphs on the screen with xml that you can scroll through vertically because they are too big to see them all. I have tried many different things but c开发者_运维问答an't figure it out.
I'm adding on to what John said above. You want a linearlayout inside a scrollview to achieve this.
Something like the following:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Paragraph 1\n\nParagraph2\n\nParagraph 3"/>
</LinearLayout>
</ScrollView>
Also, posting what you've tried so far will allow us to help you much better. Help us help you!
Embed the textviews within a linear layout, and give them some padding on the top and bottom. Either that, or combine the text into one text string and separate them with \n
.
If you're trying to add multiple textviews into one scrollview, it wont work. A scrollview container can only have one child, which is why you need to put the textviews into a linear layout, then place that linear layout into the scrollview.
Example:
<ScrollView>
<LinearLayout>
<TextView text=R.string.MyText1 />
<TextView text=R.string.MyText2 />
<TextView text=R.string.MyText3 />
</LinearLayout>
</ScrollView>
The R.string.MyText
will be your paragraphs all copied and pasted into your string resource file with the keys "MyText1", "MyText2", "MyText3", etc.
1- put the article you want to read in a .txt file
2- put it in assets Folder
3- add this to your activity.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:id="@+id/textView2"
android:maxLines = "10"
android:scrollbars = "vertical"/>
4- and set this to your TextView in the activity
TextView txtvue = (TextView) findViewById(R.id.textView2);
txtvue.setMovementMethod(new ScrollingMovementMethod());
txtvue.setText( myReader("your.txt"));
5- to get paragraphs from the .txt file use this function
public String myReader(String fileName) {
AssetManager assetManager = getResources().getAssets();
InputStream inputStream;
try {
inputStream = assetManager.open(fileName);
int size = inputStream.available();
byte[] buffer = new byte[size];
inputStream.read(buffer);
inputStream.close();
// byte buffer into a string
text = new String(buffer);
} catch (IOException e) {
e.printStackTrace();
}
return text;
}
Note: Please Do not call myReader(your.txt); in the main view thread it'll cause an error due to the lengthy process of reading text letters
精彩评论