【发布时间】:2015-05-22 06:31:06
【问题描述】:
我正在学习 Android Studio,并且我正在制作包含大量文本的程序,因此我尝试使用搜索栏使其可滚动(我也希望它可以垂直滚动)搜索栏 0 数量是顶部,100 是机器人。我做了它,以便在移动搜索栏时滚动,但我不知道如何制作它,以便它与我的文本长度相关。 这是我的 MainActivity.Java 中的代码:
seek_bar = (SeekBar)findViewById(R.id.seekBar);
text_view =(TextView)findViewById(R.id.textView);
seek_bar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
int progress_value;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress_value = progress;
TextView newtext = (TextView) findViewById(R.id.textView2);
ScrollView hscrollViewMain = (ScrollView)findViewById(R.id.scrollViev);
newtext.setText(hscrollViewMain.getMaxScrollAmount().toString()); // I made this because I thought that it would tell me the maximum amount of pixels in my text but toString() doesnt work somehow. It is redlined :(
hscrollViewMain.scrollTo(0, (hscrollViewMain.getMaxScrollAmount()/100)*progress_value); //this scrolls only little when seekbar is moved
}
这是我的activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="1">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollViev"
android:layout_gravity="center_horizontal"
android:fillViewport="false"
android:clickable="false"
android:nestedScrollingEnabled="false"
android:isScrollContainer="false">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/testtext"
android:id="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:text="@string/maintext" />
</LinearLayout>
</ScrollView>
</LinearLayout>
我也想过使用 computeVerticalScrollRange() 方法而不是 getMaxScrollAmount,但因为它是受保护的方法,我不知道如何使用它。我很困在这里,所以任何帮助将不胜感激。
【问题讨论】:
标签: android textview scrollview seekbar