2017-05-31 11 views
0

これは私のカスタムビューです(custom_view.xml):*私はCardViewに3つのマージンを定義しています。android xmlはカスタムレイアウトのマージンを考慮していません

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView 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="wrap_content" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_marginTop="8dp" 
    android:background="@color/white" 
    android:orientation="vertical" 
    app:cardCornerRadius="4dp" 
    app:cardElevation="0dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="16dp" 
     android:background="@color/white"> 

     <ImageView 
      android:id="@+id/left_image" 
      android:layout_width="64dp" 
      android:layout_height="40dp" 
      android:layout_marginTop="5dp" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_toEndOf="@id/left_image" 
      android:layout_toLeftOf="@+id/right_image" 
      android:layout_toRightOf="@id/left_image" 
      android:layout_toStartOf="@id/right_image" 
      android:orientation="vertical"> 

      <com.ringapp.ui.view.TypefaceTextView 
       android:id="@+id/title" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"/> 

      <com.ringapp.ui.view.TypefaceTextView 
       android:id="@+id/description" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       /> 

     </LinearLayout> 

     <ImageView 
      android:id="@id/right_image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginTop="5dp" 
      android:src="@drawable/icon_md_gray_arrow" /> 

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

私は、次のXML(container.xml)にカスタムレイアウトを追加します。

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

    <com.ringapp.ui.view.CustomView 
     android:id="@+id/test2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     app:description="edsc" 
     app:title="title" /> 

    <com.ringapp.ui.view.CustomView 
     android:id="@+id/test" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     app:description="asdasd" 
     app:title="asdasdasd" /> 

</LinearLayout> 

問題は、私はcustom_view.xmlに定義されたマージンが表示されないということですcontainer.xmlレイアウトに追加します。なぜこうなった? custom_view.xmlのコードをcontainer.xmlに直接貼り付けると、余白が表示されます。

+0

container.xmlにはレイアウトの幅と高さがwrap_contentのカスタムビューがあります。そして 'com.ringapp.ui.view.CustomView'で何をしていますか? – Raghunandan

+0

paddingで試してください – Pavan

+0

CustomViewはCardViewを拡張しており、私が設定した属性を読みます。 @Raghunandanの編集をチェックする – Sol

答えて

0

代わりの

<com.ringapp.ui.view.CustomView 
    android:id="@+id/test2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

この試してみてください:あなたが持っているコードを使用する場合、私はあなたが明示的に上書きする場合は特に、あなたのビューのプロパティの右側の上にいくつかのアンドロイドしようと、信じてい

<include layout="@layout/custom_view" android:id="@+id/test2/> 

をレイアウトの幅/高さ。あなたの代わりに上記のコード行を試してみてください。また、レイアウトのサイズがcustom_viewから引き継がれるため、コンテンツをラップするだけの場合は、レイアウトの幅/高さを上書きする必要はありません。

編集:レイアウト幅/高さを上書きすると、コードが機能するでしょう。しかし、他のXMLレイアウトを別のXMLレイアウトに持ってくるときにincludeを使うべきです。あなたのビューを「含む」方法は問題ありませんが、アンドロイドの「ビュー」を拡張するカスタムビュークラスを作成するときには、おそらく使用する必要があります。

関連する問題