2017-02-22 52 views
1

我在这里找到了答案,我的第一个问题,我能够成功地设定彼此相邻的我的两个按钮:xml文件:按钮旁边的对方 - 但每个人都有自己的侧

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_alignParentTop="false" 
    android:layout_alignParentLeft="false" 
    android:layout_alignParentStart="false" 
    android:layout_alignParentBottom="false" 
    android:layout_alignParentRight="false" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:layout_below="@id/description" 
    android:layout_alignParentEnd="false"> 

    <!-- Send it Button --> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/confirm_it" 
     android:textColor="@color/white" 
     android:textSize="15sp" 
     android:layout_weight="0" 
     android:id="@+id/confirm_button" 
     android:backgroundTint="@color/darkCyan" 
     android:onClick="sendName" 
     /> 

    <!-- Skip it Button --> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/skip_it" 
     android:textColor="@color/white" 
     android:textSize="15sp" 
     android:id="@+id/skip_button" 
     android:backgroundTint="@color/darkCyan" 
     android:layout_marginLeft="10dp" 
     android:layout_marginStart="10dp" 
     android:onClick="skipName" 
     /> 
    </LinearLayout> 

现在我想要做别的事情,我仍然希望两个按钮相邻,但是我希望每个例如SEND按钮都位于页面的最左侧,SKIP按钮位于页面的右侧:

这是我想要的:

enter image description here

可能吗?如何?

回答

0

最简单的答案是更换LinearLayoutRelativeLayout并使用对齐

<RelativeLayout ...> 

    <Button ... 
     android:layout_alignParentLeft="true" /> 
    <Button ... 
     android:layout_alignParentRight="true" /> 

</RelativeLayout> 
+0

正是我所需要的。谢谢! – morkuk

0

尼克·卡多佐的答案是正确的,干净,我个人会使用该方法。如果由于某种原因,你绝对需要的LinearLayout可以实现您通过以下方式问:

  1. 你的两个按钮之间添加一个空的视图(0dp宽度和0dp高度)。
  2. 将其设置为“layout_weight”property to 。

下面是一些代码,以帮助您

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

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SEND"/> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SKIP"/> 

</LinearLayout> 

这里是您与此代码得到的结果:

Click for Image!

+0

谢谢Claff,我会使用它,并且我同意,在我的项目中,有时我需要一个LinearLayout,所以它会很有用。 – morkuk

0

如果你必须使用的LinearLayout。您可以尝试使用的LinearLayout重量或weightSum

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

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_weight="1" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1" 
    android:orientation="horizontal"> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".5"/> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight=".5" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 
0

还有更简单的方法。如果您在两个按钮之间添加布局,并且布局权重为“1”。两个按钮按需要对齐。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:minWidth="25px" 
     android:minHeight="25px" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/linearLayout1"> 
     <Button 
      android:text="Button" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/button1" /> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:minWidth="25px" 
      android:minHeight="25px" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/linearLayout2" 
      android:layout_weight="1" /> <!-- I say there --> 
     <Button 
      android:text="Button" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/button2" /> 
    </LinearLayout> 
</LinearLayout> 
相关问题