2

他のコンテンツに関係なく、ボタンや他のコンテンツを画面の下部に固定するにはどうすればよいですか?

私は次のようなレイアウトを持っています:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:gravity="center_horizontal" >

            <TextView ... />

            ...

            <TextView ... />

        </LinearLayout>

        <Button
            android:id="@+id/ButtonMain"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="@string/button_text"
            android:textSize="30px"
            android:textStyle="bold"
            android:width="200px" />

    </LinearLayout>

</ScrollView>

そして、ButtonMain の下部が常に画面の下部にあるようにします。ただし、ボタンはリニアレイアウトのコンテンツのすぐ下にレンダリングされ、画面の下部にはレンダリングされません。layout_gravity="center_horizontal|bottom"ボタンを独自のLinearLayout内に同様の重力で配置するだけでなく、試しましたが、うまくいきませんでした。

このレイアウトを実現するにはどうすればよいですか?

4

4 に答える 4

2

LinearLayout の代わりに RelativeLayout を使用できます。以下のタグを追加することで画面のボタンに疎外されます

android:layout_alignParentBottom="true"

ボタンを常に一番下に配置したい場合は、ScrollView を RelativeLayout 内に配置し、ボタンを一番下に配置する必要があります

参照: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

于 2012-07-31T19:22:51.613 に答える
1

上記のコメントによると、OP はButton彼のScrollView. ただし、XML はルート要素を 1 つしか持てないため、何らかのレイアウトでScrollViewandをラップする必要があります。Button

初期のレイアウトと寸法を使用して、以下にそれを示しました。

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

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true" >

        <!-- You can remove one of these two LinearLayouts, since they just nest each other. Which one you want to remove is up to you. -->
        <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:layout_gravity="center_horizontal"
                android:gravity="center_horizontal" >

                <TextView ... />

                ...

                <TextView ... />

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/ButtonMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text="@string/button_text"
        android:textSize="30px"
        android:textStyle="bold"
        android:width="200px" />

</LinearLayout>

何か問題がありましたらお知らせください。

于 2012-08-01T00:24:26.337 に答える
1

Scroll ビューを相対レイアウトでラップしますが、xml ファイル内の Buttons を最初に alignParentBottom フィールドを true に設定します。次に、scrollView を入れて、ボタンの上に設定します。次に、スクロールビューが残りのスペースを埋め、常にボタンの上に表示されます。ボタンは常に表示され、スクロールしません。

于 2012-07-31T19:22:54.817 に答える
1
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txt1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Hello"
            android:textSize="25sp" />

        <TextView
            android:id="@+id/txt2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Hello2"
            android:textSize="25sp" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="bottom|center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/ButtonMain"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:text="click me"
                android:textSize="30px"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>
于 2012-07-31T19:27:18.197 に答える