[英]Unable to scroll Linear Layout with ScrollView
我是Android新手。 我正在创建一个添加用户的表单。 由于表单字段很多,我需要向下滚动。 我读到ScrollView仅采用一个子视图。 我已经将视图包装在线性布局中,这是ScrollView的直接子级。
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:animateLayoutChanges="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText... />
<EditText.../>
<EditText... />
<EditText.../>
<LinearLayout... >
<LinearLayout... >
<LinearLayout... >
<TextView... />
</LinearLayout>
<ScrollView/>
将滚动视图的xml更改为此
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
您需要添加一些属性:
1)到您的ScrollView:
android:fillViewport="true"
2)和您的LinearLayout:
android:descendantFocusability="blocksDescendants"
例:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardDetailScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/card_detail_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
Your Content...
</LinearLayout>
</ScrollView>
使用以下代码
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 7" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 8" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 9" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 10" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 11" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 12" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 13" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 14" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 15" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 16" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 17" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 18" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 19" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 20" />
</LinearLayout>
</ScrollView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.