android gravity属性 跟 weight属性
android gravity属性 和 weight属性
有点忘记这两个属性了,复习一下。
来看这个布局文件
这是一个很正常的布局文件了,效果如下。
![android gravity属性 跟 weight属性 android gravity属性 跟 weight属性](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEyLzA2LzI4LzAzMjAyNjIwNDguanBn)
当我们给这三个EditText设置上gravity属性之后效果如下,先看 xml文件。
![android gravity属性 跟 weight属性 android gravity属性 跟 weight属性](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEyLzA2LzI4LzAzMjAyNjIwNDkuanBn)
由此可以看出gravity属性是用来控制 EditText里边文本的位置。
我们现在对xml文件做如下修改
运行效果如下
![android gravity属性 跟 weight属性 android gravity属性 跟 weight属性](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEyLzA2LzI4LzAzMjAyNjIwNTAuanBn)
感觉上这个android:layout_weight="1.0"是用来设置控件的大小,因为经过我们的设置中间的那个EditText变大了。其它两个EditText的 android:layout_weight属性我们没有设置,没有设置就会使用默认值, 默认值为 0.0。 设置为1.0的那个控件会填充剩余空白的部分。
我们如果想要3个组件均等地共享空间,应该将他们的weight属性都设置为 1.0,这样将均匀的扩展每一个EditText。
![android gravity属性 跟 weight属性 android gravity属性 跟 weight属性](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDEyLzA2LzI4LzAzMjAyNjIwNTEuanBn)
复习完。
有点忘记这两个属性了,复习一下。
来看这个布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="one"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="two"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="three"/> </LinearLayout>
这是一个很正常的布局文件了,效果如下。
当我们给这三个EditText设置上gravity属性之后效果如下,先看 xml文件。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:text="one"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="two"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="three"/> </LinearLayout>
由此可以看出gravity属性是用来控制 EditText里边文本的位置。
我们现在对xml文件做如下修改
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:text="one"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1.0" android:text="two"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="three"/> </LinearLayout>
运行效果如下
感觉上这个android:layout_weight="1.0"是用来设置控件的大小,因为经过我们的设置中间的那个EditText变大了。其它两个EditText的 android:layout_weight属性我们没有设置,没有设置就会使用默认值, 默认值为 0.0。 设置为1.0的那个控件会填充剩余空白的部分。
我们如果想要3个组件均等地共享空间,应该将他们的weight属性都设置为 1.0,这样将均匀的扩展每一个EditText。
1 楼
hillmickey
2011-08-26
android:gravity是用来设置控件里面的子控件等的对齐方式的
android:layout_gravity是用来设置控件本身在它的父控件中的对齐方式!!!!
android:layout_gravity是用来设置控件本身在它的父控件中的对齐方式!!!!