2017-07-15 30 views
0

私はアンドロイドを初めて使っています。レイアウトの実際の仕組みやデバッグ方法がわかりません。これは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> 

によって覆われています。それはこのようになりますImageViewのなし

enter image description here

enter image description here

+0

相対的なレイアウトを使用することができますか? –

答えて

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を使用する必要があります)。ImageViewCardViewの全部を占め、TextViewはスペースを取れません。

両方の子供にはandroid:layout_height="0dp"を設定し、それぞれにはどれくらいのスペースをとるかを指定するにはandorid:layout_weightを使用する必要があります。

2

代わりのリニアレイアウトあなたはImageViewのを削除するとどうなりますか

<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> 
関連する問題