xml - 屏幕角落的按钮

标签 xml android-layout button

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

<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 作为 subview (按钮)的父级,并且希望两个按钮都位于两侧角,请更新您的代码,如下所示:

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

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

享受编码...:)

关于xml - 屏幕角落的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28670957/

相关文章:

java - 从 String 中删除空格,然后转换为 XML

c# - 在 C# 程序中替代数据库的好选择

java - 如何使用安卓:layout_weight consistently for all screensizes

android - 通过android :fontFamily to custom view in Android

ios - 如何在按钮触摸时填充 UITableView

java - 为什么不能在 android 中用工具栏覆盖操作栏?

Android: strings.xml 的默认语言

android - 应用程序不使用全屏宽度(Actionbar)

javascript - Jquery onclick 导航上一个按钮

java - 单击按钮Android后绘制