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

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

概述我正在学习AndroidStudio,我正在编写非常多文本的程序,因此我试图使用seekbar使其可滚动(我希望它可以垂直滚动)seekbars0amount是top,100是bot.我做了它,以便在移动搜索栏时滚动但我无法弄清楚如何制作它以便它与我的文本的长度相关.这是我的MainActivity.Java中的代码:s

我正在学习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

<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)在Seekbar的左/右创建一个relativelayout

2)在这个relativeLayout中创建一个TextVIEw

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

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

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

希望能帮助到你

总结

以上是内存溢出为你收集整理的android – 如何使用搜索栏滚动文本?全部内容,希望文章能够帮你解决android – 如何使用搜索栏滚动文本?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1117153.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存