画面にたくさんのアイテムがあり、ユーザーが下にスクロールできるようにスクロールバーを使用する必要があります。ただし、スクロールが表示されないか、機能していません。スクロールバーをに追加するにはどうすればよいLinearLayout
ですか?
10 に答える
線形レイアウトを<ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
</LinearLayout>
注:APIレベル8以降では、 fill_parentは非推奨になり、match_parentに名前が変更されました。
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
線形レイアウトをスクロールビューでラップする必要があります
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
これが私が試行錯誤してやった方法です。
ScrollView - (the outer wrapper).
LinearLayout (child-1).
LinearLayout (child-1a).
LinearLayout (child-1b).
ScrollViewは子を1つしか持つことができないため、その子は線形レイアウトです。次に、他のすべてのレイアウトタイプが最初の線形レイアウトで発生します。私はまだ相対的なレイアウトを含めようとはしていませんが、それらは私を狂わせるので、私の正気が戻るまで待ちます。
これは、タグを使用して実行できます<ScrollView>
。ScrollViewの場合、注意しなければならないことの1つは、ScrollViewには1つの子が必要です。
完全なレイアウトをスクロール可能にする場合<ScrollView>
は、上部に追加します。以下の例を確認してください。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
ただし、レイアウトの一部をスクロール可能にする<ScrollView>
場合は、その部分に追加します。以下の例を確認してください。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
</LinearLayout>
ScrollViewをレイアウトファイルの最初の子として配置し、その中にlinearlayoutを配置する必要があります。これで、Androidは、利用可能なコンテンツとデバイスサイズに基づいて、スクロール可能かどうかを決定します。
ScrollViewは複数の子を持つことができないため、linearlayoutに兄弟がないことを確認してください。
次の属性を使用して、線形レイアウトで囲む必要があります
<LinearLayout ...>
<scrollView ...>
</scrollView>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<---------Content Here --------------->
</LinearLayout>
</ScrollView>
</LinearLayout>
レイアウトをスクロール可能にしたいときはいつでも<ScrollView>
、レイアウトまたはコンポーネントをその中に使用できます。
linearLayoutに属性を追加できます:android:scrollbars="vertical"