如何使用 SeekBar 滚动文本?

4

我正在学习Android Studio,正在制作一个包含大量文本的程序,因此我尝试使用SeekBar使其可滚动(我还希望它可以垂直滚动),SeekBar的0表示顶部,100表示底部。我已经做到了当SeekBar移动时滚动文本,但我无法弄清如何将其与我的文本长度相关联。 以下是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,但由于它是受保护的方法,我不知道该如何使用它。我在这里遇到了困难,所以非常感谢任何帮助。

1个回答

1
我认为你可以这样做:
1)在SeekBar的左侧/右侧创建一个RelativeLayout
2)在此RelativeLayout中创建一个TextView
3)现在您必须获取RelativeLayout的高度。要做到这一点,您可以使用onMeasure/onLayout(但是这样您必须实现自定义RelativeLayout)或者简单地使用ViewTreeObserver 4)现在您已经拥有了两个组件的高度,所以进行计算
5)每次移动SeekBar时,您都需要“更新”TextView位置。在RelativeLayout内,您可以使用children.layout(left, top, right, bottom)设置位置,然后调用TextView上的invalidate()来更新它。
希望对您有所帮助

感谢您的回复!很抱歉这个问题耽误了您的时间,因为我所在的地区停电了,而且我不会在星期天编程。但是无论如何,我用子对象相对视图制作了滚动视图。里面有seekbar和textview,但我不明白的是如何使用onMeasure/onLayout。我的意思是它们都是受保护的方法,而且我在代码中有这个RelativeLayout Relativelay = (RelativeLayout)findViewById(R.id.Relativel); 这不是自定义RelativeLayout,对吗?我尝试从互联网上搜索如何在Java中使用受保护的方法,但并没有真正理解如何做到这一点。 - JLatvus
要获取布局测量值,您需要使用ViewTreeObserver。http://www.limbaniandroid.com/2014/10/viewtreeobserver-how-to-get-layout.html - Stefano Vuerich
谢谢,问题已解决。虽然没有直接起作用,但稍加修改就实现了我需要的功能。如果其他人遇到同样的问题,我删除了这一行代码:this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 它使布局变成了红色,然后我在this之前添加了final:final LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout); - JLatvus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接