屏幕角落的按钮

Buttons at corners of the screen

我需要在左上角和右上角放置两个按钮。但是我得到了左下角的两个按钮。如何更正代码?请帮忙。

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

        <Button
            android:id="@+id/back_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:background="@drawable/new_back_button"
            android:contentDescription="@string/LeaveReq_back_button" />

        <Button
            android:id="@+id/logout_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/logout"
            android:contentDescription="@string/logout_button"
            android:gravity="right" />
</LinearLayout>

对于 LinearLayout,视图会根据其方向并排放置

您可以使用 RelativeLayoutalign_parent 来实现您的布局

这是一个例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/back_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/new_back_button"
    android:contentDescription="@string/LeaveReq_back_button" />

<Button
    android:id="@+id/logout_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/back_button"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/logout"
    android:contentDescription="@string/logout_button" />

如果您想将 LinearLayout 用作子视图(按钮)的父视图,并且希望两个按钮都位于两个边角,请像下面这样更新您的代码:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="@string/hello_world"
        android:contentDescription="LeaveReq_back_button" />
    <View android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/logout_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="logout_button"
        android:gravity="right" />
</LinearLayout>

如果您允许将 LinearLayout 的父级更新为 RelativeLayout,那么您可以像下面这样更新它:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/back_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/new_back_button"
    android:contentDescription="@string/LeaveReq_back_button" />

<Button
    android:id="@+id/logout_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/back_button"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/logout"
    android:contentDescription="@string/logout_button" />
</RelativeLayout>

现在由您决定如何使用它。

享受编码...:)