2017-07-15 35 views
0

我是新來的android,我不知道佈局是如何工作或如何調試。這是CardView xml文件的外觀。我知道文本在那裏。但是,由圖像TextView沒有出現在cardView上

<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:id="@+id/card_view" 
android:layout_width="match_parent" 
android:layout_height="200dp" 
android:layout_margin="5dp" 
card_view:cardElevation="2dp" 
card_view:cardCornerRadius="4dp"> 

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

    <ImageView 
     android:id="@+id/info_image" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:scaleType="centerCrop"/> 

    <TextView 
     android:id="@+id/info_text" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

覆蓋這是怎麼樣子的應用程序。

enter image description here

沒有它看起來像這樣的ImageView的。

enter image description here

+0

會發生什麼事,如果你刪除的ImageView? –

回答

2

儘量不要使用FILL_PARENT不贊成這種方式,並使用佈局的重量也顯示文本。以下是有關正確使用layout_weight Android Developers的更多信息。

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/card_view" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_margin="5dp" 
     card_view:cardCornerRadius="4dp" 
     card_view:cardElevation="2dp"> 

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

      <ImageView 
       android:id="@+id/info_image" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:scaleType="centerCrop" /> 

      <TextView 
       android:id="@+id/info_text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

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

謝謝!它修復了它,現在看起來很棒。 –

2
android:layout_height="match_parent" 

該屬性使視圖以填充整個父視圖。 (請注意,它與fill_parent相同,應使用API​​ 8中引入的match_parent)。這會導致ImageView填充整個CardView,並且不允許TextView佔用任何空間。

您應該爲兩個孩子設置android:layout_height="0dp",並使用andorid:layout_weight來指定每個應該佔用多少空間。

2

代替線性佈局,你可以使用相對佈局

<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:id="@+id/card_view" 
android:layout_width="match_parent" 
android:layout_height="200dp" 
android:layout_margin="5dp" 
card_view:cardElevation="2dp" 
card_view:cardCornerRadius="4dp"> 

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/info_image" 
    android:layout_alignParentTop="true" 
    android:layout_height="match_parent" 
    android:layout_width="wrap_content" 
    android:scaleType="centerCrop"/> 

<TextView 
    android:layout_below="@id/info_image" 
    android:id="@+id/info_text" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</RelativeLayout>