【问题标题】:How to scroll text with seekbar?如何使用搜索栏滚动文本?
【发布时间】: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


    【解决方案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() 来更新它。

    希望对你有帮助

    【讨论】:

    • 感谢您的回复!很抱歉这花了太长时间,但我所在的地区停电了,我周日不编程。但无论如何,我用子对象 relativeview 制作了 Scrollview。里面有seekbar和textview,但我不明白的是如何使用这个onMeasure/onLayout。我的意思是它们都是受保护的方法,我在代码 RelativeLayout Relativelay = (RelativeLayout)findViewById(R.id.Relativel);那不是自定义的RelativeLayout,对吗?我试图从 Internet 上搜索有关在 Java 中使用受保护方法的信息,但并没有真正了解如何去做。
    • 要获得布局度量,您需要使用 ViewTreeObserver limbaniandroid.com/2014/10/…
    • 谢谢你! (y) 解决了问题。没有直接工作,但几乎没有修改它做了我需要的。如果其他人遇到同样的问题,我删除了这一行 this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);它使布局变红,然后我添加了 final : final LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
    猜你喜欢
    • 2020-04-11
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多