January 17th, 2012, 02:53 AM
|
#2 (permalink)
|
|
Premium Member
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
|
Use TableLayout when you want Android to calculate column width for you. If you want to control column widths, don't use TableLayout.
Here's a implementation of your layout. It uses a top-level vertical linear layout. Inside it are three nested layouts. First is a relative layout, then 2 horizontal linear layouts.
Notice that I moved the margin between the relative layout and the first linear layout onto the first linear layout itself.
Also notice that I moved the constrains on the button heights to being a constraint on the linear layouts. This is cleaner.
Your table layout would have worked for what is now the relative layout. But I converted it to a relative layout because: a) this is an ideal use of the relative layout, and b) to demonstrate the relative layout.
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/imageView2"
android:layout_toRightOf="@+id/imageView1"
android:gravity="center"
android:text="Hello World - Long Title Demonstration"
android:textColor="#000000" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dip"
android:layout_marginTop="30dip"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="New With Long Label That Wraps" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Share" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dip"
android:orientation="horizontal" >
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Open" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Comm" />
</LinearLayout>
</LinearLayout>
|
|
|