android 搜索文本,android – 如何使用搜索栏滚动文本?

我正在学习Android Studio,我正在编写非常多文本的程序,因此我试图使用seekbar使其可滚动(我希望它可以垂直滚动)seekbars 0 amount是top,100是bot.我做了它,以便在移动搜索栏时滚动但我无法弄清楚如何制作它以便它与我的文本的长度相关.

这是我的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

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">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/seekBar" />

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">

android:id="@+id/linearLayout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/testtext"

android:id="@+id/textView2" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/textView"

android:text="@string/maintext" />

我还想过使用computeVerticalScrollRange()方法而不是getMaxScrollAmount,但因为它是受保护的方法,我不知道如何使用它.我非常喜欢这里所以任何帮助将非常感激.

解决方法:

在我的意见中你可以这样做:

1)在Seekbar的左/右创建一个Relativelayout

2)在这个RelativeLayout中创建一个TextView

3)现在你必须得到RelativeLayout的高度.要做到这一点,你可以使用onMeasure / onLayout(但这样你必须实现自定义RelativeLayout)或只是一个ViewTreeObserver

4)现在您拥有两个组件的高度,以便进行计算

5)每次移动SeekBar时,都必须“更新”TextView位置.在RelativeLayout内部,您可以使用children.layout(左,上,右,下)设置位置,然后在TextView上调用invalidate()来更新它.

希望能帮助到你

标签:android,textview,seekbar,scrollview

来源: https://codeday.me/bug/20190708/1400609.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值