layout_gravity 和 gravity 的区别

android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠这个属性设置,不设置默认是在左侧的。

android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。比如说button:

  • android:gravity表示button上的字在button上的位置;
  • android:layout_gravity 表示按钮在界面上的位置。

可选值

top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical,这些属性是可以多选的,用“|”分开。默认这个的值是:Gravity.LEFT

image

<?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”
>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”gravity水平居中”
android:textSize=”20dip”
android:gravity=”center_horizontal” 
    android:background=”#666″ />
<ImageButton
  android:layout_marginTop=”10dip” 
android:layout_marginBottom=”10dip” 
android:id=”@+id/ImageButton01″
android:src=”@drawable/android”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
 android:layout_gravity=”center_horizontal”>
</ImageButton>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”right”
    android:text=”layout_gravity局右”
android:textSize=”20dip”
android:background=”#666″ />
</LinearLayout>

 

对于LinearLayout何时生效

  • 当 android:orientation=”vertical”  时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的
  • 当 android:orientation=”horizontal” 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

在EditText中文字居中显示,而EditText组件自己右对齐到了LinearLayout

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<EditText
android:layout_width=”wrap_content”
android:gravity=”center”
android:layout_height=”wrap_content”
android:text=”one”
android:layout_gravity=”right”/>
</LinearLayout>

通过Java代码来设置组件的位置

Button button = new Button(this);
button.setText(“One”);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//此处相当于布局文件中的Android:layout_gravity属性
lp.gravity = Gravity.RIGHT;
button.setLayoutParams(lp);
//此处相当于布局文件中的Android:gravity属性
button.setGravity(Gravity.CENTER);
LinearLayout linear = new LinearLayout(this);
//注意,对于LinearLayout布局来说,设置横向还是纵向是必须的!否则就看不到效果了。
linear.setOrientation(LinearLayout.VERTICAL);
linear.addView(button);
setContentView(linear);

设置在RelativeLayout中的位置addRule

params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mContainer.addView(progress,params);

via http://www.cnblogs.com/ghj1976/archive/2011/04/26/2029535.html

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: