Android XML - 如何让项目在最左边、中间和最右边对齐

Posted

技术标签:

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

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

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

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

</LinearLayout>

【问题讨论】:

【参考方案1】:

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

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

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

【讨论】:

【参考方案2】:

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

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

    <Button android:id="@+id/Button01" 
              android:layout_ 
              android:layout_ 
              android:text="Cancel"
              android:layout_ 
              android:layout_weight="2" >
      </Button>
      <TextView android:id="@+id/TextView01" 
                android:layout_ 
                android:layout_ 
                android:text="New Place"
                android:layout_ 
                android:layout_weight="1" >
      </TextView>
      <Button android:id="@+id/Button03" 
              android:layout_
              android:layout_
              android:text="Save"
              android:layout_ 
              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_
android:layout_>

<TableRow android:layout_
android:layout_
android:layout_margin="5dp">

<Button android:id="@+id/Button01"
    android:layout_
    android:layout_
    android:text="Cancel"
    android:layout_marginRight="30dp"
    android:layout_marginLeft="10dp">
</Button>

<TextView android:id="@+id/TextView01"
    android:layout_
    android:layout_
    android:text="New Place"
    android:layout_gravity="center_horizontal"
    android:layout_marginRight="30dp">
</TextView>

<Button android:id="@+id/Button03"
    android:layout_
    android:layout_
    android:text="Save"
    android:layout_gravity="right">
</Button> 
</TableRow> 
</LinearLayout>

【讨论】:

以上是关于Android XML - 如何让项目在最左边、中间和最右边对齐的主要内容,如果未能解决你的问题,请参考以下文章

如何让extjs window 总在最前面显示

latex 如何一行中插入多个图片且只在最左边加标注

怎么让 dom4j 添加的节点 在最前面``而不是最后面吗~

TextView能否实现图片在左边,文字在右边,并且水平居中?

css中水平并列有三个div块元素,左边的div给了左浮动,可为啥左边哪个没有在最左?

android 如何让imageview一直在最顶层,小尺寸图片,像淘宝客户端那种工具栏按钮