2016-07-15 74 views
0

我想创建一个类似于在一个回收视图cardview内的列表。如何避免嵌套的LinearLayout或RelativeLayout

这是一个应该在回收站适配器的getholderview函数中以编程方式填充的详细框。

现在我想出了下面的代码,但不知何故,我觉得这不是很高效。

我怎样才能避免很多嵌套的布局来实现这一点,我应该甚至试图避免在这种情况下呢?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:card_view="http://schemas.android.com/apk/res-auto" 
       xmlns:app="http://schemas.android.com/tools" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

    <android.support.v7.widget.CardView 

     <RelativeLayout ...> 
      ... Other content Title 
<ImageView 
      android:id="@+id/profileImage" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:background="@drawable/ic_loyalty_black_24dp" 
      /> 

<TextView 
    android:id="@+id/cell_textView" 
    android:layout_toRightOf="@id/profileImage" 
    android:layout_toEndOf="@id/profileImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:focusable="true" 
    android:clickable="true" 
    android:foreground="?android:attr/selectableItemBackground" 
    android:textAppearance="?android:attr/textAppearanceMedium"/> 

<TextView 
    android:id="@+id/fat_bar_text" 
    android:layout_toRightOf="@id/profileImage" 
    android:layout_toEndOf="@id/profileImage" 
    android:layout_below="@id/cell_textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Fett:" 
    android:layout_marginTop="3dp" 
    android:textAppearance="?android:attr/textAppearanceSmall"/> 

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          android:id="@+id/detailBox" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:orientation="vertical" 
          android:layout_below="@id/fat_bar_text" 
          android:gravity="bottom"> 

       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <TextView 
         android:layout_gravity="start" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="0.5" 
         android:text="Value:"/> 

        <TextView 
         android:layout_weight="0.5" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="500€"/> 
       </LinearLayout> 

       <LinearLayout 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <TextView 
         android:layout_gravity="start" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="0.5" 
         android:text="Test:"/> 

        <TextView 
         android:layout_weight="0.5" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="blah"/> 
       </LinearLayout> 
      </LinearLayout> 
     </RelativeLayout> 


    </android.support.v7.widget.CardView> 
</LinearLayout> 

回答

2

根LinearLayout是不必要的,使用CardView作为根元素。

如果您没有触摸事件或样式,则第二个LinearLayout也是不必要的。与子元素

你可以把它包含两个TextView的成另一个XML文件,并将它们包括与<include>标签的最内层的LinearLayout layout_below ATTR取代它,这样可以使你的代码更简单,干净。并且易于重复使用。这样

mytext.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_gravity="start" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.5" 
     android:text="Value:"/> 

    <TextView 
     android:layout_weight="0.5" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="500€"/> 
</LinearLayout> 

cardview.xml

<include 
    layout="@layout/mytext" 
    android:id="@+id/mytext1" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<include 
    layout="@layout/mytext" 
    android:id="@+id/mytext2" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 
+0

谢谢,我试图删除第二个但后来的意见不再定位了,请看编辑的问题。 – cinatic

+0

我不明白?你是否在第二个LinearLayout的子视图中设置了android:below? – Harlan

+0

你的意思是android:layout_below,studio在自动完成时不会给我这个选项,我应该设置它的什么id,cardview的id? – cinatic

2

请考虑使用Constraint Layout。它的创建旨在为任何复杂性的建筑布局提供灵活性。它减少了嵌套布局的数量。 One more link

+0

thx这看起来很有趣 – cinatic