2015-02-23 132 views
1

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

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

查看我的回答低于 – 2015-02-23 10:25:58

回答

2

如果您想使用的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> 

现在它取决于你,你如何使用它。

享受编码... :)

+0

@VeronikaGilbert欢迎 – 2015-02-23 10:41:11

0

随着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" />