Android - хочу оптимизировать макет - PullRequest
1 голос
/ 23 февраля 2011

Я создал макет ниже, используя Linear Layout, он работает нормально, но у меня есть одно сомнение, что его можно оптимизировать, потому что я создал этот пользовательский интерфейс с использованием вложенного Linear Layout, я думаю, что это не очень хорошо.

Итак, не могли бы вы помочь мне оптимизировать этот линейный макет с помощью относительного макета или еще как-нибудь?

Я определил макет, например:

<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>

Ответы [ 5 ]

2 голосов
/ 23 февраля 2011

Вот рефакторинг вашего кода с использованием 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>  

Поскольку вы сказали, что хотите использовать его в качестве строки в ListView, я изменил высоту родительского макета на "wrap_content".

1 голос
/ 23 февраля 2011

Вы можете попробовать это, но я не думаю, что вам нужно это

<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>
0 голосов
/ 19 июля 2014

возможно, вы могли бы использовать относительное расположение вместо линейного, потому что для такого не очень сложного пользовательского интерфейса относительное расположение имеет лучшую производительность.

0 голосов
/ 04 марта 2011
<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>
0 голосов
/ 23 февраля 2011

Надеюсь, это поможет

<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"
/>  

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...