Android的 - 要优化布局 [英] Android - Want to optimize Layout
问题描述
我已经用线性布局,它的运行良好的布局之下,但我有一个疑问,它可以因为我已经使用嵌套的线性布局创造了这个UI进行优化,我觉得它不是很好。
所以,你能帮帮通过使用相对布局要不然我优化这个线性布局?
我已经定义了一个类似的布局:
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:填充=6DIP> <的LinearLayout
机器人:方向=垂直
机器人:layout_width =WRAP_CONTENT
机器人:layout_weight =1
机器人:layout_height =FILL_PARENT
机器人:layout_marginLeft =10dip> <的TextView
机器人:ID =@ + ID / txtViewTitle
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:重力=center_vertical
机器人:TEXTSIZE =18dip
机器人:文字=你好
/>
<的TextView
机器人:ID =@ + ID / txtViewDescription
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:重力=center_vertical
机器人:TEXTSIZE =14dip
机器人:文字=你好
/>
< / LinearLayout中> < ImageView的
机器人:ID =@ + ID /图像
机器人:layout_width =50dip
机器人:layout_height =FILL_PARENT
机器人:SRC =@绘制/箭头
机器人:layout_marginLeft =3dip
机器人:scaleType =中心/>< / LinearLayout中>
下面是一个使用RelativeLayout的您code的重构:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
机器人:填充=6DIP>
< ImageView的机器人:layout_marginLeft =3dip机器人:ID =@ + ID /图像
机器人:SRC =@绘制/ arrow_cell
机器人:layout_width =50dip机器人:scaleType =中心
机器人:layout_alignParentRight =真正的机器人:layout_height =WRAP_CONTENT机器人:layout_centerVertical =真正的>< / ImageView的>
< TextView的机器人:TEXTSIZE =18dip机器人:layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT机器人:ID =@ + ID / txtViewTitle
机器人:layout_marginLeft =10dip机器人:文本=你好
机器人:重力=center_vertical机器人:layout_alignParentLeft =真
机器人:layout_toLeftOf =@ + ID /图像>< / TextView的>
< TextView的机器人:TEXTSIZE =14dip机器人:layout_height =WRAP_CONTENT
机器人:layout_width =FILL_PARENT机器人:ID =@ + ID / txtViewDescription
机器人:layout_marginLeft =10dip机器人:文本=hello2
机器人:重力=center_vertical机器人:layout_below =@ + ID / txtViewTitle
机器人:layout_toLeftOf =@ + ID /图像>< / TextView的>
< / RelativeLayout的>
至于你说你想使用它作为一个ListView一排,我改变了父布局的HIGHT为WRAP_CONTENT。
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"
android: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".
这篇关于Android的 - 要优化布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!