首页 > 其他分享 >Android TextView 设置内容可滚动

Android TextView 设置内容可滚动

时间:2022-11-04 12:06:29浏览次数:48  
标签:layout vertical parent text match 滚动 Android android TextView


前言

开发中scrollBar 用的最多的地方就是在内容超过显示区域后,可以手动上下左右滑动来查看
解决方案比较多。

方案一

使用一个可滑动的组件ScroolView包裹用于在内容超过显示区域后可滑动的布局。限制一个固定高度即可实现

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tv_bottom"
android:layout_marginLeft="3.5dp"
android:layout_marginTop="40dp"
android:layout_marginRight="3.5dp"
android:layout_marginBottom="6dp"
android:background="@color/white">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="30dp"
android:ellipsize="end"
android:lineSpacingExtra="8dp"
android:text="填入可滑动内容...."
android:textColor="@color/color_4a4a4a"
android:textSize="13sp" />

</LinearLayout>

</ScrollView>
方案二

设置TextView 的Scrollbars 属性为vertical。(共三个属性[horizontal,vertical,none])。在
Activity界面中 findViewById 找到Textview组件。调用
public final void setMovementMethod(MovementMethod movement) { } ,设置MovementMethod。

<TextView
android:scrollbarStyle="outsideInset"
android:id="@+id/txt_scroll"
android:scrollbars="vertical"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:text="@string/world_get_ingral_dailog_one"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val text = findViewById<TextView>(R.id.txt_scroll)
text.movementMethod = ScrollingMovementMethod.getInstance()
}
}

效果如下图。均可以实现滑动。

Android TextView 设置内容可滚动_android


系统默认scrollBar为渐隐效果,如果需要常显设置 android:fadeScrollbars=“false”,即可


标签:layout,vertical,parent,text,match,滚动,Android,android,TextView
From: https://blog.51cto.com/u_15861646/5823426

相关文章

  • Android Compose 修饰符类行为整理
    前言compose修饰符用来对界面组件装饰,类似ViewTree系统的组件属性,compose对于修饰符用法做了限制,RowScope,ColumScope,BoxScope,等等限制为仅可以在这些组件使用的修饰符......
  • Android LayoutParam,MarginLayoutParams
    前言开发中经常会遇到一个场景,给View动态设置margin边距,针对容器类布局比较直观。对非容器类进行margin边距设置需按不同的LayoutParams设置,否则很容造成异常。问题:为......
  • Android 使用 unity 导出obb包
    1.通过unity导出包含obb的工程。2.按照google官方给定的obb命名方式,已经存放路径进行操作​​Obb命名方式​​命名方式:[main|patch]。<扩展版本>。<程序包名称>.obbeg......
  • Android LocalBroadcastManager 使用
    前言LocalBroadcastManager简单使用。1.注册【添加IntentFilter】2.反注册3.发广播publicclassMain2ActivityextendsAppCompatActivity{@Overrideprotect......
  • Android 英文数字混排导致提前换行完美解决
    前言数字加英文混排造成,段落提前换行异常。网上可找到处理方式较多。处理方式:1.自定义TextView,测量文字宽度与父窗体宽度自行进行人为换行占主流。2.全角半角进行统一,将字......
  • android studio 4.1变更
    前言这两天被androidstudio4.1升级后遇到的问题折腾的头大。虽然自己遇到的问题和网友遇到的问题不一样。总结一句话。升级需谨慎问题归问题,这次更新还是有很多亮点。官......
  • Android 基础 MaterialButton
    项目中经常会使用到,给按钮添加边框,点击效果,圆角,icon+文字圆角。发现系统就有提供好的组件,除了CardView可以设置。androidmaterialdesign支持库中各种可以直接拿来用的组......
  • android 手机 apk安装失败对应码
    下面是从网上找到的几种常见的错误及解决方法:1、INSTALL_FAILED_INVALID_APK:无效的安装包,安装包已损坏请检查安装包是否完整。如果是xpk包,可以通过手动安装xpk来检测一......
  • Android 共享内存(ashmem)持续更新
    Android共享内存(ashmem)前言项目中接入讯飞语音合成,在sdk中看到MemoryFile,了解下用法发现,看到的只是冰山一角。官方介绍:SharedMemoryenablesthecreation,mapping,and......
  • Android kotlin泛型知识点梳理
    前言学习知识需要提前设立目标,带着问题学习才能有的放矢。无论是java的泛型还是kotlin语言的泛型均是写框架,写通用工具类神器。如果不熟悉泛型语法,开发过程中将会遇到很多奇......