【问题标题】:Android XML - how to get items aligned far left, center, and far rightAndroid XML - 如何让项目在最左边、中间和最右边对齐
【发布时间】:2011-02-12 05:33:24
【问题描述】:

我有这个 XML 代码,它生成一个按钮、一个 textview 和另一个按钮,我该如何让按钮出现在最左边、中间的 textview 和最右边的最后一个按钮?

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button android:id="@+id/Button01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Cancel">
    </Button>
    <TextView android:id="@+id/TextView01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="New Place">
    </TextView>
    <Button android:id="@+id/Button03" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save">
     </Button>

</LinearLayout>

【问题讨论】:

    标签: xml android layout alignment


    【解决方案1】:

    我会推荐使用RelativeLayout,它让事情变得更容易。

    通过使用 RelativeLayout,您可以将 textview 设置为在父级中水平居中或在父级中居中

    这样做之后,您可以通过将这些按钮与父侧对齐或将它们直接连接到 textview 的侧边来将这些按钮直接附加到远端,也许添加一点填充,然后就可以得到您的间距。

    【讨论】:

      【解决方案2】:

      您需要使用 weight 属性。可以将其视为让 android 知道它应该为每个项目提供的宽度百分比。

      您需要将所有 3 个项目的宽度设置为 0dip 并添加权重属性

          <Button android:id="@+id/Button01" 
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content" 
                    android:text="Cancel"
                    android:layout_width="0dip" 
                    android:layout_weight="2" >
            </Button>
            <TextView android:id="@+id/TextView01" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content" 
                      android:text="New Place"
                      android:layout_width="0dip" 
                      android:layout_weight="1" >
            </TextView>
            <Button android:id="@+id/Button03" 
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Save"
                    android:layout_width="0dip" 
                    android:layout_weight="2" >
            </Button>
      

      玩转权重值,你就会明白它是如何工作的 :)

      在此之后,您可以使用重力属性将文本移动到中心/左侧或右侧。

      【讨论】:

        【解决方案3】:

        如果你想保留LinearLayout,你要做的是:创建一个TableRow,然后把所有的项目(按钮和Textview)都放进去。这将创建一条直线,所有物品整齐排列。这段代码对我有用,抱歉我不能截屏(新的,声望点不够),但它对我有用,希望对你有帮助。

        <?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">
        
        <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
        
        <Button android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:layout_marginRight="30dp"
            android:layout_marginLeft="10dp">
        </Button>
        
        <TextView android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Place"
            android:layout_gravity="center_horizontal"
            android:layout_marginRight="30dp">
        </TextView>
        
        <Button android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save"
            android:layout_gravity="right">
        </Button> 
        </TableRow> 
        </LinearLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-01-13
          • 2022-01-20
          • 2017-09-29
          • 1970-01-01
          • 2021-12-20
          • 2015-06-17
          相关资源
          最近更新 更多