Android ScrollView 疑问

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="@color/colorPrimaryDark"
        android:text="测试一下测试一下测试一下测试一下测试一下测试一下测试一下测试一下测试一下测试一下测试一下"
        android:textSize="70sp" />

</ScrollView>

clipboard.png

阅读 2k
1 个回答

图片描述

scrollview实际宽高在代码中~=(约等于)屏幕宽高,验证方法,可以在Activity中输出scrollview的高度 实际是约等于屏幕高度,而不是内容高度

然后它还有content宽高,这个宽高是根据内容的大小决定。
android:layout_gravity="center"是让TextView的中心对齐scrollview的中心,并不能对齐scrollview的content中心

如果想实现对齐content中心 建议在TextView外层包裹一个LinearLayout或者其他ViewGroup
代码如下

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:id="@+id/scroll">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@color/colorPrimaryDark"
            android:text="测试一下测试一下测试一下测试一下测试一下测试一下测试一下测试一下测试一下测试一下测试一下"
            android:textSize="70sp" />
    </LinearLayout>

</ScrollView>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题