问题
I have a string from a file that am storing into 3 separate TextViews because I was having alignments issues with a single TextView. How can I scroll only this section of my screen to see all of the contents of the file?
BufferedReader buf = new BufferedReader(new FileReader(file));
while((line = buf.readLine())!= null) {
StringTokenizer st = new StringTokenizer(line);
a = st.nextToken();
b = st.nextToken();
c = st.nextToken();
text1.append(a + '\n');
text2.append(b + '\n');
text3.append(c + '\n');
}
dp1.setText(text1);
dp2.setText(text2);
dp3.setText(text3);
回答1:
You can use the following xml with a few tweaks if needed,
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/dp1"... />
<TextView android:id="@+id/dp2"... />
<TextView android:id="@+id/dp3"... />
</HorizontalScrollView>
And now in your java code, you can access them as,
TextView textV1 = (TextView)findViewById(R.id.dp1);
TextView textV2 = (TextView)findViewById(R.id.dp2);
TextView textV3 = (TextView)findViewById(R.id.dp3);
textV1.setText(text1);
textV2.setText(text2);
textV3.setText(text3);
if you want you can use vertical scroll bars as,
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="vertical"
android:layout_height="set the height here" >
</ScrollView>
回答2:
You can place your TextViews inside a HorizontalScrollView in your XML file, like this
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView ... />
<TextView ... />
<TextView ... />
</HorizontalScrollView>
回答3:
set android:ellipsize="marquee" for your textViews in layout file and setSelected(true) in your code where your are setting text. I Hope this will help you.
来源:https://stackoverflow.com/questions/8113738/scroll-multiple-textviews-simultaneously