2016-10-05 134 views
0

我正在尝试创建三个CardView,每个包含一些ImageViewTextView。我没有使用RecyclerView,因为我将始终在这个活动中使用这3 CardViews。但为什么会显示CardView的内容(如预览和应用程序)?Android - CardView内容未显示

这就是我得到: enter image description here

下面是活动我的XML代码。我在strings.xml中设置了相应的值。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.yankee.cw.ll_home"> 

    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     app:cardUseCompatPadding="true" 
     android:id="@+id/card_view0" 
     android:layout_gravity="center" 
     android:padding="16dp" 
     android:clickable="true" 
     card_view:contentPadding="24dp" 
     android:foreground="?android:attr/selectableItemBackground" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     card_view:cardBackgroundColor="@color/cardview_light_background" 
     card_view:cardElevation="@dimen/cardview_default_elevation" 
     card_view:cardCornerRadius="4dp"> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:weightSum="1" 
      android:layout_height="0dp"> 
      <ImageView 
       android:layout_width="0dp" 
       android:layout_weight="0.15" 
       android:layout_height="match_parent" /> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="0.70" 
       android:layout_gravity="center" 
       android:textAllCaps="true" 
       android:text="@string/ll_home_card_title_1" 
       android:layout_height="match_parent" /> 
      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="0.15"/> 
     </LinearLayout> 
     <TextView 
      android:layout_width="match_parent" 
      android:text="@string/ll_home_card_content_1" 
      android:layout_height="0dp" /> 
    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     app:cardUseCompatPadding="true" 
     android:id="@+id/card_view1" 
     android:layout_gravity="center" 
     android:padding="16dp" 
     android:clickable="true" 
     android:foreground="?android:attr/selectableItemBackground" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     card_view:cardBackgroundColor="@color/cardview_light_background" 
     card_view:cardElevation="@dimen/cardview_default_elevation" 
     card_view:cardCornerRadius="4dp"> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:weightSum="1" 
      android:layout_height="0dp"> 
      <ImageView 
       android:layout_width="0dp" 
       android:layout_weight="0.15" 
       android:layout_height="match_parent" /> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="0.70" 
       android:layout_gravity="center" 
       android:textAllCaps="true" 
       android:text="@string/ll_home_card_title_2" 
       android:layout_height="match_parent" /> 
      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="0.15"/> 
     </LinearLayout> 
     <TextView 
      android:layout_width="match_parent" 
      android:text="@string/ll_home_card_content_2" 
      android:layout_height="0dp" /> 
    </android.support.v7.widget.CardView> 

    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     app:cardUseCompatPadding="true" 
     android:id="@+id/card_view2" 
     android:layout_gravity="center" 
     android:clickable="true" 
     android:foreground="?android:attr/selectableItemBackground" 
     android:padding="16dp" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     card_view:cardBackgroundColor="@color/cardview_light_background" 
     card_view:cardCornerRadius="4dp"> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:weightSum="1" 
      android:layout_height="0dp"> 
      <ImageView 
       android:layout_width="0dp" 
       android:layout_weight="0.15" 
       android:layout_height="match_parent" /> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="0.70" 
       android:layout_gravity="center" 
       android:textAllCaps="true" 
       android:text="@string/ll_home_card_title_3" 
       android:layout_height="match_parent" /> 
      <ImageView 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="0.15"/> 
     </LinearLayout> 
     <TextView 
      android:layout_width="match_parent" 
      android:text="@string/ll_home_card_content_3" 
      android:layout_height="0dp" /> 

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

</LinearLayout> 
</ScrollView> 
+1

设置线性布局和textview高度android:layout_height =“0dp”来包装内容。 –

+0

尝试将scrollview更改为nestedscrollview,也可以删除卡视图周围的线性布局,可以实现填充,没有它的边距 –

+0

@KevinCrain谢谢,好的,我会试试。 – Yankee

回答

2

你需要设置高度的LinearLayout到match_parent,不0dp, 并在每个CardView最后的TextView被设置为0的高度,所以你应该改变它,你可以使用权内的LinearLayout但不是在它(除非它在其他线性布局内)

+0

感谢LinearLayout中使用权重的信息。 – Yankee