2015-12-30 96 views

回答

1

我還沒有實現一個RecyclerView,但是我已經在一個線性佈局複製兩行:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:background="#303030" 
     android:id="@+id/cv1" 
     card_view:cardElevation="5dp" 
     android:foreground="?android:attr/selectableItemBackground" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="80dp" 
      android:layout_margin="4dp" 
      android:longClickable="true" 
      android:background="#303030"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/person_photo" 
       android:background="@drawable/vector_red" 
       android:layout_alignBottom="@+id/txtSub" /> 


      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:text="Large Text" 
       android:id="@+id/txtMain" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/person_photo" 
       android:layout_toEndOf="@+id/person_photo" 
       android:elevation="4dp" 
       android:textSize="20dp" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="Small Text" 
       android:id="@+id/txtSub" 
       android:layout_below="@+id/txtMain" 
       android:layout_toRightOf="@+id/person_photo" 
       android:layout_toEndOf="@+id/person_photo" /> 

     </RelativeLayout> 

    </android.support.v7.widget.CardView> 
    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:background="#303030" 
     android:id="@+id/cv1" 
     card_view:cardElevation="5dp" 
     android:foreground="?android:attr/selectableItemBackground" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="80dp" 
      android:layout_margin="4dp" 
      android:longClickable="true" 
      android:background="#303030"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/person_photo" 
       android:background="@drawable/vector_red" 
       android:layout_alignBottom="@+id/txtSub" /> 


      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:text="Large Text" 
       android:id="@+id/txtMain" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/person_photo" 
       android:layout_toEndOf="@+id/person_photo" 
       android:elevation="4dp" 
       android:textSize="20dp" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="Small Text" 
       android:id="@+id/txtSub" 
       android:layout_below="@+id/txtMain" 
       android:layout_toRightOf="@+id/person_photo" 
       android:layout_toEndOf="@+id/person_photo" /> 

     </RelativeLayout> 

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

</LinearLayout> 

通盤考慮,您可能需要更改視圖id的

讓我們進一步解釋這一點:

這是一個帶有垂直方向的LinearLayout,這意味着LinearLayout中的元素將會一個接一個地垂直放置。這是它的要點。

如果您想要更好地控制項目的放置方式,請考慮在根處使用RelativeLayout。

0

您可以創建一個可容納卡片的佈局。這種接近方式在將來需要添加或移除卡片時提供了靈活性。

preference.xml

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/white_alpha" 
     android:orientation="vertical" 
     android:title="@string/your_title"> 

     <include layout="@layout/first_card"/> 

     <include layout="@layout/second_card"/> 

    </LinearLayout> 
</ScrollView> 

接下來,建立兩個證佈局:

first.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
           xmlns:app="http://schemas.android.com/apk/res-auto" 
           xmlns:tools="http://schemas.android.com/tools" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content" 
           android:layout_gravity="center_horizontal" 
           android:layout_marginBottom="8dp" 
           app:cardBackgroundColor="@color/white" 
           app:cardCornerRadius="0dp" 
           app:cardElevation="8dp" 
           tools:showIn="@layout/preference"> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:paddingTop="10dp" 
     android:paddingStart="15dp" 
     android:text="@string/your_text" 
     android:textColor="@color/color" 
     android:textSize="14sp" 
     android:textStyle="bold"/> 

</LinearLayout> 

second.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
           xmlns:app="http://schemas.android.com/apk/res-auto" 
           xmlns:tools="http://schemas.android.com/tools" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content" 
           android:layout_gravity="center_horizontal" 
           android:layout_marginBottom="8dp" 
           app:cardBackgroundColor="@color/white" 
           app:cardCornerRadius="0dp" 
           app:cardElevation="8dp" 
           tools:showIn="@layout/preference"> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:paddingTop="10dp" 
     android:paddingStart="15dp" 
     android:text="@string/your_text" 
     android:textColor="@color/color" 
     android:textSize="14sp" 
     android:textStyle="bold"/> 

</LinearLayout> 

相關問題