如何在Android上使用和不使用屏幕按钮的设备上放置视图?

时间:2014-02-28 22:19:59

标签: android android-layout button

我使用相对布局来显示我的主UI。在这个布局中,底部有3个按钮。它基本上是这样的:

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

<!-- Some other content  ... -->

<!-- These are the 3 buttons at the bottom -->

<ImageButton
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentBottom="true"
    android:contentDescription="@string/button1"
    android:background="@drawable/button1"
    android:src="@drawable/button1" />

<ImageButton 
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:contentDescription="@string/button2"
    android:background="@drawable/button2"
    android:src="@drawable/button2" />

<ImageButton
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:contentDescription="@string/button3"
    android:background="@drawable/button3"
    android:src="@drawable/button3" />

</RelativeLayout>

现在,我在我的应用程序中也使用全屏布局(就像Android KitKat中的方式一样)。这意味着UI在导航栏/屏幕按钮后面流动。

我试图实现的是:

  • 对于已启用屏幕按钮的设备 3个按钮应位于屏幕按钮的正上方:

  • 对于没有屏幕按钮的设备: 3个按钮应位于屏幕底部。

任何想法如何做到这一点?我可以使用fitSystemWindows吗?

1 个答案:

答案 0 :(得分:0)

相当简单。使用FrameLayout,第一个孩子将是你的应用程序ui在LinearLayout内。第二个孩子将成为LinearLayout内的app按钮栏。我们将使用加权来确保按钮栏始终位于设备屏幕的底部。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:id="@+id/regularLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        ...
    </LinearLayout>
    <LinearLayout
        android:id="@+id/buttonLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
        <LinearLayout
            android:id="@+id/actualButtonBar"
            android:layout_width="match_parent"
            android:layout_height="60dp" >
        </LinearLayout>
    </LinearLayout>
</FrameLayout>