【问题标题】:How can I position views on devices with and without on screen buttons in Android?如何在 Android 中使用和不使用屏幕按钮的设备上定位视图?
【发布时间】:2014-03-01 06:29:51
【问题描述】:

我正在使用相对布局来显示我的主 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 吗?

【问题讨论】:

    标签: android android-layout button


    【解决方案1】:

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

    <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>
    

    【讨论】:

    • 您需要重写您的问题,以便我进一步帮助您。
    • 您需要什么信息?您的布局根本没有达到我在问题中描述的内容
    猜你喜欢
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    相关资源
    最近更新 更多