AndroidではHorizontalScrollViewの横スライドレイアウトを使用

6056 ワード

Androidでは、ListViewは通常、縦スクロールのリストを実現するために使用され、HorizontalScrollViewは横スクロールのリスト項目を実現することができます.
LinearLayoutなどのHorizontalScrollViewコントロールの導入は簡単です.
<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" >

    <HorizontalScrollView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> 
    HorizontalScrollView>

LinearLayout>

このとき、サブコントロールは追加されていないので、IDEは「This HorizontalScrollView view is useless(no children,no background,no id,no style)」というメッセージを表示します.
例として、このHorizontalScrollViewにいくつかのボタンを追加してみます.
<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="com.example.viewholdertest.MainActivity" >

    <HorizontalScrollView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"/>

        ......(     )

    HorizontalScrollView>

LinearLayout>

IDEは「A scroll view can have only one child」と提示します.本来、HorizontalScrollViewには、直接的なサブViewが1つしか存在しない.そのため、LinearLayoutのようなコントロールを使用して、複数の子Viewを挿入し、変更したレイアウトファイルは次のようになります.
<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="com.example.viewholdertest.MainActivity" >

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello" />

            ......(     )
        LinearLayout>
    HorizontalScrollView>
LinearLayout>

注意HorizontalScrollViewのLinearLayoutのwitdhプロパティは「wrap_content”. アプリを実行すると、横スライドで余分なボタンが表示されます.