14

我見過很多應用程序中使用分隔線的CardViews(我不確定它們是否爲CardViews),所以我猜測有一個簡單的方法來創建它們。 我想問一下它是如何完成的?那些甚至是CardViews帶有分隔線的Cardview

我無法找到更多關於它們的信息,因爲我完全不知道View的名稱,所以如果有人能指示我用代碼示例,我會很感激。

圖片例如: enter image description here

+0

我認爲這是在列表中有背景和分頻器參數時listItems。 – Meenaxi

回答

17

可以使用此代碼這可能有助於

<android.support.v7.widget.CardView 
     android:id="@+id/cardview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/margin_large" 
     android:layout_marginRight="@dimen/margin_large" 
     android:elevation="100dp" 
     card_view:cardBackgroundColor="@android:color/white" 
     card_view:cardCornerRadius="8dp"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 
       android:gravity="center_vertical" 
       android:paddingLeft="25dp"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Conversations" /> 
      </LinearLayout> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="2dp" 
       android:background="@android:color/darker_gray" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingLeft="30dp" 
       android:paddingTop="20dp" 
       android:paddingBottom="10dp"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="game" /> 
        ... 
      </LinearLayout> 
     </LinearLayout> 
    </android.support.v7.widget.CardView> 
+0

請回答此問題:http://stackoverflow.com/questions/36393540/no-shadow-elevation-underneath-second-card-if-there-are-two-card-in-the-layout – 2016-04-04 03:08:01

+0

簡單而不錯 –

9

所示出的屏幕截圖示出了具有視圖作爲分頻器在-之間的法線CardView。 如果你搜索這樣的東西,沒有DividerView或類似的東西。只需使用具有高度和背景的簡單View

我在我的圖書館裏有類似的東西。我使用它來創建分頻器:

<View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="@color/stroke"/> 

card_library.xml

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:gravity="center_vertical" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/libraryname" 
      style="@style/CardTitle" 
      android:fontFamily="sans-serif-condensed" 
      android:textStyle="normal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:maxLines="1"/> 

     <TextView 
      android:id="@+id/libraryversion" 
      style="@style/CardTitle" 
      android:fontFamily="sans-serif-condensed" 
      android:textStyle="normal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="4dp" 
      android:layout_marginTop="4dp" 
      android:gravity="left|bottom" 
      android:maxLines="1" 
      android:textSize="12sp"/> 

     <TextView 
      android:id="@+id/librarycreator" 
      style="@style/CardTitle" 
      android:fontFamily="sans-serif-condensed" 
      android:textStyle="normal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="2dp" 
      android:gravity="right" 
      android:maxLines="2" 
      android:textSize="14sp"/> 
    </LinearLayout> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_marginTop="4dp" 
     android:background="@color/stroke"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:padding="4dp"> 

     <TextView 
      android:id="@+id/description" 
      style="@style/CardText" 
      android:fontFamily="sans-serif-condensed" 
      android:textStyle="normal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:maxLines="20"> 
     </TextView> 
    </LinearLayout> 

</LinearLayout> 

那麼它會是這樣的:

Divider

+0

請回答這個:http://stackoverflow.com/questions/36393540/no-shadow-elevation-underneath-second-card-if-there-are-two-card-in-the-layout – 2016-04-04 03:08:13

+0

你使用什麼顏色? – Caipivara

1

套裝應用程序:cardElevation =「0dp」,然後使用視圖 - 見下文

<android.support.v7.widget.CardView 
android:id="@+id/cv" 
xmlns:android="http://schemas.android.com/apk/res/android" 
app:cardElevation="0dp" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<!-- Insert UI elements --> 

    <View 
     android:layout_width="fill_parent" 
     android:background="@android:color/darker_gray" 
     android:layout_height="2dp"/> 

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