HorizontalScrollView内容显示不全
问题重现
在项目中使用HorizontalScrollView控件时,出现了一个极其奇葩的布局问题,HorizontalScrollView内的最右侧的部分子View无法全部显示,且看如下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:paddingTop="20dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="测试测试测试" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="按钮" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Preview效果展示:
Preview
看上去没什么问题,但是当运行程序后在真机中却发现,HorizontalScrollView可正常滚动,但是无法显示全部,右侧的子View无法显示:
HorizontalScrollView显示不全
问题解决
后来在论坛上查找关于这方面的解决方案,但是寥寥无几,仅有一条还能有所帮助,就是在代码中动态添加HorizontalScrollView的子View LinearLayout,但是我并没有尝试,一来太麻烦,二来我认为问题的关键不是出在代码还是xml布局。
后来通过摸索发现,真正的罪魁祸首是给HorizontalScrollView的前一个View——TextView设置了android:layout_weight="1"权重,而且同时设置了margin后,HorizontalScrollView右侧溢出的宽度更大了。
当在xml布局中定位到HorizontalScrollView标签时,Preview中显示的HorizontalScrollView宽度出现了异常,宽度溢出了。HorizontalScrollView的子View LinearLayout溢出显示无可厚非,但是HorizontalScrollView的大小怎么也不会溢出,所以问题就出现在了这里。
HorizontalScrollView宽度溢出
此时尝试取消TextView的权重,定位到HorizontalScrollView后没有了溢出的情况:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="测试测试测试" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
...
</LinearLayout>
</HorizontalScrollView>
取消TextView权重
Android SDK中的坑太多,需要大家不断的去埋。
由此,可能与HorizontalScrollView同级的View设置了权重,会直接影响HorizontalScrollView或ScrollView的布局显示。