Android: gravity + match_parent+fill_parent+wrap_

android:gravity sets the gravity of the content of the View its used on.

  1. center
  2. center_vertical
  3. center_horizontal, etc.

android:layout_gravity sets the gravity of the View or Layout in its parent.

That is, android:gravity positions what’s inside the view,  whereas android:layout_gravity positions the view with what the view is contained in.

LinearLayout width “wrap_content”, it’s essentially “hugging” the two views inside so they have no space to center themselves in:

Change the width to “fill_parent”: fill the entire width of the screen, its contents have room to actually center themselves and so see what happens:

FILL_PARENT (is replaced by MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding) the only option for forward compatibility is MATCH_PARENT

How to do for  TextView staying to the left and the ImageView going to the right:

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
<LinearLayout
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” >
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”left”
android:text=”@string/hello” />
<!– New Layout –>
<LinearLayout
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:gravity=”right” >
<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/icon” />
</LinearLayout>
</LinearLayout>
</LinearLayout>

difference between layout_gravity and gravity:

TextView is centering itself vertically within its parent LinearLayout: 

android:layout_gravity=”center”

<!– layout_gravity example –>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
<LinearLayout
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”200px” >
<TextView
android:layout_width=”wrap_content”
android:layout_height=”100px”
android:layout_gravity=”center”
android:text=”@string/hello” />
</LinearLayout>
</LinearLayout>

android:gravity=”center”

drawing saving pass a Bitmap into the Canvas constructor, then use the compress method of Bitmap to create an OutputStream which can be written to the SD card.

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: