ScrollView常见问题(不能填满屏幕、内部layout_weight无效、进入自动下滑问题等)

不能填满屏幕、内部layout_weight无效--> android:fillViewport="true"

这个布局意图是ScrollView内部有两个TextView,并且textView高度各占屏幕一半

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

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

       <TextView
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="@color/colorAccent"/>

       <TextView
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:background="@color/colorPrimaryDark"/>
   </LinearLayout>
</ScrollView>

然而效果却如下:

为ScrollView加上android:fillViewport="true",再次运行效果如下:

进入自动下滑问题

当ScrollView包含recyclerView时可能会自动下滑到recyclerView处,因为recyclerView抢夺了其他控件的焦点,我们可以在ScrollView包裹的第一个布局加上

 android:focusable="true"
 android:focusableInTouchMode="true"

猜你喜欢

转载自blog.csdn.net/Mr_Leixiansheng/article/details/84989620
今日推荐