2016-08-07 5 views
0

私はAndroidアプリを開発しています。私のアプリでは、イメージにオーバーレイを設定しようとしています。しかし、相対レイアウト内のビューのサイズは、withとheightで常に0です。match_parentをwidthとheightの両方に設定しても、常にゼロになります。下記の私のシナリオをご覧ください。AndroidのRelativeLayoutで表示幅と高さが常にゼロです

これはこれは

enter image description here

あなたが見ることができるように結果である

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_marginTop="5dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_width="match_parent" 
    android:id="@+id/di_card_container" 
    android:layout_height="wrap_content"> 

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

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

     <RelativeLayout 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentBottom="true" 
      android:id="@+id/di_name_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <View 
       android:layout_centerInParent="true" 
       android:background="@color/blue" 
       android:id="@+id/destination_item_overlay" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 
      <TextView 
       android:padding="10dp" 
       android:textSize="15dp" 
       android:textColor="@color/white" 
       android:id="@+id/di_tv_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </RelativeLayout> 
    </RelativeLayout> 
</android.support.v7.widget.CardView> 

私のXMLレイアウトで幅や視野の高さがあるため、名前には青色の背景が相対ではありませんレイアウトはゼロです。しかし、以下のように明示的に幅と高さを設定すると、動作しています。

<View 
        android:layout_centerInParent="true" 
        android:background="@color/blue" 
        android:id="@+id/destination_item_overlay" 
        android:layout_width="300dp" 
        android:layout_height="30dp"/> 

ただし、すべてのデバイスと互換性がありません。なぜwithとheightは常にゼロですか?私はそのビューをプログラムで制御する必要があるので、そのビューを追加しています。その背景ビューの幅と高さを親に合わせて設定するにはどうすればよいですか?

答えて

2

あなたは循環依存関係があるためです。 <RelativeLayout>は子供に身長を伝えるように求めており、<View>は親に身長と同じように伝えています。高さを修正するとすぐに問題を解決できます。

ここにこれを試してみてください:そんなに

 <RelativeLayout 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentBottom="true" 
      android:id="@+id/di_name_container" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <View 
       android:layout_centerInParent="true" 
       android:background="@color/blue" 
       android:id="@+id/destination_item_overlay" 
       android:layout_width="match_parent" 
       android:layout_alightTop="@id/di_tv_name" 
       android:layout_alightBottom="@id/di_tv_name" 
       android:layout_height="match_parent"/> 
      <TextView 
       android:padding="10dp" 
       android:textSize="15dp" 
       android:textColor="@color/white" 
       android:id="@+id/di_tv_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
     </RelativeLayout> 
+0

感謝を。それは完璧に働いた。 –

+0

Cool。喜んで助けになる – Shaishav

関連する問題