2017-05-31 56 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具有一個customview,其佈局寬度和高度與wrap_content相同。你在'com.ringapp.ui.view.CustomView'中做了什麼? – Raghunandan

+0

嘗試填充 – 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/> 

我相信當你使用你的代碼,Android的嘗試過右一些視圖的性能,尤其是當你明確地覆蓋佈局寬度/高度。嘗試使用上述代碼行代替你的代碼。此外,如果您只是打算包裝內容,則無需覆蓋版面寬度/高度,因爲版面尺寸將從您的custom_view中引入。

編輯:爲了澄清,如果您取出佈局寬度/高度覆蓋,您的代碼甚至可能工作。但是,當將其他XML佈局引入不同的XML佈局時,您應該使用include。 「包含」視圖的方式很好,但在編寫擴展android'視圖'的自定義視圖類時應該可以使用。