具有粘性页脚的Android相对布局

Joe*_*oey 7 xml layout android relativelayout android-layout

我对Android开发相对较新,而且我很难将某个界面组合在一起.我查看了许多类似的问题,但没有一个给我我正在寻找的答案.

我想把一个XML接口放在一起,如下所示:

在此输入图像描述

我想要一个LinearLayout,它就像一个包含两个按钮的粘性页脚.该部分将始终与底部对齐,并且始终为60dp高.然后,RelativeLayout将位于页脚顶部,但它的高度应根据屏幕大小进行缩放.

我尝试过以下方法:

<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=".DartboardActivity">

    <RelativeLayout 
            android:id="@+id/game_layout"
            android:layout_width="match_parent"
            android:layout_height="506dp"
            android:background="@drawable/background"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">  

            <ImageView
                android:id="@+id/Dartboard"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/desc_dartboard"
                android:src="@drawable/dartboard" />

            <hhs.week3.dartboard.VerticalSeekBar
                android:id="@+id/seekBarVertical"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="bottom"
                android:thumb="@drawable/thumbhorizontal"
                android:layout_marginBottom="40dp" />

             <SeekBar
                android:id="@+id/seekBarHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:thumb="@drawable/thumbhorizontal" />

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#333">

        <Button
            android:id="@+id/fire"
            style="@style/button"
            android:layout_width="0dp"

            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:text="@string/fire" />

        <Button
            android:id="@+id/settings"
            style="@style/button"
            android:layout_width="0dp"

            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:text="@string/settings" />

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我让它工作得很好,但它需要我给RelativeLayout位一个固定的高度,使它看起来在我的手机上,但不是在其他人.

我该如何最好地接近这个?

laa*_*lto 15

使用该LinearLayout layout_weight机制为您的所有剩余空间分配RelativeLayout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        ... />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp" 
        ... />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Chi*_*hod 6

请尝试以下代码

<RelativeLayout 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=".DartboardActivity" >

    <RelativeLayout
        android:id="@+id/game_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttons"
        android:layout_alignParentTop="true"
        android:background="@drawable/ic_launcher"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="#333"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我删除LinearLayout并替换为RelativeLayout.这样您就可以将LinearLayout操纵杆设置在底部.