Android的XML - 如何获取项目排列最左边,中间和最右边 [英] Android XML - how to get items aligned far left, center, and far right

查看:321
本文介绍了Android的XML - 如何获取项目排列最左边,中间和最右边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的XML code产生一个​​按钮,一个TextView和其他按钮,我该如何去获得该按钮出现在最左边,TextView的在中心,在最右边的最后一个按钮?

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout机器人:ID =@ + ID / LinearLayout01
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <按钮机器人:ID =@ + ID / Button01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=取消>
    < /按钮>
    < TextView的机器人:ID =@ + ID / TextView01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=新广场>
    < / TextView的>
    <按钮机器人:ID =@ + ID / Button03
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=保存>
     < /按钮>

< / LinearLayout中>
 

解决方案

您需要使用重属性。把它看成是让机器人知道宽度,它应该给每个项目的百分比。

您需要设置宽度0dip所有3项,并添加重量属性

 <按钮机器人:ID =@ + ID / Button01
              机器人:layout_width =WRAP_CONTENT
              机器人:layout_height =WRAP_CONTENT
              机器人:文本=取消
              机器人:layout_width =0dip
              机器人:layout_weight =2>
      < /按钮>
      < TextView的机器人:ID =@ + ID / TextView01
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=新的地方
                机器人:layout_width =0dip
                机器人:layout_weight =1>
      < / TextView的>
      <按钮机器人:ID =@ + ID / Button03
              机器人:layout_width =WRAP_CONTENT
              机器人:layout_height =WRAP_CONTENT
              机器人:文本=保存
              机器人:layout_width =0dip
              机器人:layout_weight =2>
      < /按钮>
 

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

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

I have this XML code which generates a button, a textview and another button, How do I go about getting the button to appear in far left, the textview in the center and the last button on the far right?

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

解决方案

You need to use the weight property. Think of it as letting android know the percentage of width it should give each item.

you need to set width to 0dip for all the 3 items and add the weight property

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

Play around with the weight value and you will understand how it works :)

After this you can use the gravity property to move text to center / left or right.

这篇关于Android的XML - 如何获取项目排列最左边,中间和最右边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆