我想创建一个类似于在一个回收视图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>
谢谢,我试图删除第二个但后来的意见不再定位了,请看编辑的问题。 – cinatic
我不明白?你是否在第二个LinearLayout的子视图中设置了android:below? – Harlan
你的意思是android:layout_below,studio在自动完成时不会给我这个选项,我应该设置它的什么id,cardview的id? – cinatic