2017-05-31 35 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'视图'的自定义视图类时应该可以使用。