[英]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.