2014-08-27 64 views
0

我需要为列表视图项目创建一个布局,如下所示。但我试图不去创造它,但我也在考虑表现。我尝试过使用带有weight属性的嵌套线性布局来做到这一点,但据我所知,使用具有这种属性的嵌套线性布局并不是一个好习惯,因为它会降低我的应用程序的性能。此外,我试图使用相对布局,但我没有达到预期的结果。中间文本视图应该提供最多的空间。 在此先感谢您的帮助。创建列表项目的最佳做法

enter image description here

+0

整个项目的高度是已知的还是固定的?中间的文本框每个都必须占据垂直空间的一半吗? – 2014-08-27 22:22:38

回答

0

也许我没有理解错的话,也许-or有一定的要求,你没有说,但我认为它应该满足您的需求:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="100dp" 
android:orientation="horizontal" 
android:weightSum="1"> 

<RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="0.25" 
    android:background="#8E7AE5"></RelativeLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="0.5" 
    android:background="#E57A7A" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="#D5E57A" 
     android:gravity="center" 
     android:text="Top" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:gravity="center" 
     android:text="Down" /> 
</LinearLayout> 


<RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="0.25" 
    android:background="#8E7AE5"></RelativeLayout> 
</LinearLayout> 
0

嗯,我会做这样的:

<LinearLayout xmlns:andoird="http://schemas.android.com/apk/res/android" 
andoird:layout_width="match_parent" 
andoird:layout_height="48dp" 
andoird:orientation="horizontal" 
android:weightSum:1> 


<TextView 
    andoird:layout_width="0dp" 
    andoird:layout_height="match_parent" 
    andoird:layout_weight="0.25" 
    andoird:text="Left textView"/> 

<!-- Middle container --> 
<LinearLayout 
    andoird:layout_width="0dp" 
    andoird:layout_height="wrap_content" 
    andoird:layout_weight="0.5" 
    andoird:orientation="vertical"> 

    <TextView 
     andoird:layout_width="match_parent" 
     andoird:layout_height="24dp" 
     andoird:text="Middle Top textView"/> 

    <TextView 
     andoird:layout_width="match_parent" 
     andoird:layout_height="24dp" 
     andoird:text="Middle Bottom textView"/> 

</LinearLayout> 

<Switch 
    andoird:layout_width="0dp" 
    andoird:layout_height="match_parent" 
    andoird:layout_weight="0.25" 
    andoird:text="Right switch" /> 

ŧ他的布局不会抛出关于性能的任何XML警告。说到性能,你应该在你的ListView适配器中使用你的ViewHolder。它有助于很多。

0

试试这个方法,希望这会帮助你解决你的问题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.25" 
     android:gravity="center_vertical" 
     android:paddingLeft="10dp"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="textview"/> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.50" 
     android:orientation="vertical" 
     android:paddingLeft="10dp"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:gravity="center_vertical"> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="textview"/> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:gravity="center_vertical"> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="textview"/> 
     </LinearLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.25" 
     android:gravity="center_vertical" 
     android:paddingLeft="10dp"> 
     <Switch 
      android:id="@+id/main_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 
</LinearLayout> 

注:我想使用的LinearLayout体重就可以达到您的要求。