I have created the below layout using Linear Layout , its running fine but i have one doubt that it can be optimized because i have created this UI using nested Linear Layout , i think its not well.
So Can you please help me to optimize this Linear Layout either by using Relative Layout or else ?
I have defined a layout like:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
an开发者_高级运维droid:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip">
<TextView
android:id="@+id/txtViewTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="18dip"
android:text="hello"
/>
<TextView
android:id="@+id/txtViewDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="14dip"
android:text="hello"
/>
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:src="@drawable/arrow"
android:layout_marginLeft="3dip"
android:scaleType="center"/>
</LinearLayout>
Here is a refactoring of your code using RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="6dip">
<ImageView android:layout_marginLeft="3dip" android:id="@+id/image"
android:src="@drawable/arrow_cell"
android:layout_width="50dip" android:scaleType="center"
android:layout_alignParentRight="true" android:layout_height="wrap_content" android:layout_centerVertical="true"></ImageView>
<TextView android:textSize="18dip" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/txtViewTitle"
android:layout_marginLeft="10dip" android:text="hello"
android:gravity="center_vertical" android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/image"></TextView>
<TextView android:textSize="14dip" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/txtViewDescription"
android:layout_marginLeft="10dip" android:text="hello2"
android:gravity="center_vertical" android:layout_below="@+id/txtViewTitle"
android:layout_toLeftOf="@+id/image"></TextView>
</RelativeLayout>
As you said you want to use it as a row in a ListView, I changed the hight of the parent layout to "wrap_content".
You can try this but I dont think you need this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="6dip">
<TextView android:id="@+id/txtViewTitle" android:layout_width="250dip"
android:layout_height="200dip" android:gravity="center_vertical"
android:textSize="18dip" android:text="hello" />
<TextView android:id="@+id/txtViewDescription"
android:layout_below="@+id/txtViewTitle" android:layout_width="250dip"
android:layout_height="200dip" android:gravity="center_vertical"
android:textSize="14dip" android:text="hello" />
<ImageView android:id="@+id/image" android:layout_width="50dip"
android:layout_height="fill_parent" android:layout_marginLeft="3dip"
android:scaleType="center" android:layout_alignParentRight="true" /> </RelativeLayout>
Hope this helps
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dip">
<ImageView android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="3dip"
android:scaleType="center"
android:src="@drawable/arrow"
android:gravity="center" />
<TextView android:id="@+id/txtViewDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="14dip"
android:text="hello"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/image"
/>
<TextView android:id="@+id/txtViewTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="18dip"
android:text="hello"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/image"
android:layout_above="@+id/txtViewDescription"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txtViewTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="18dip"
android:text="hello"
/>
<TextView
android:id="@+id/txtViewDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="14dip"
android:text="hello"
/>
<ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:src="@drawable/arrow"
android:layout_marginLeft="3dip"
android:scaleType="center"/>
</LinearLayout>
maybe you could use the relativelayout to replace the linearlayout,because for such an not very complicated UI,the relativelayout has the better performance.
精彩评论