0

我制作了一个复合控件,但是当我将此新组件添加到具有其他标准组件(例如 TextView)的 XML 层中时,当该层膨胀时,复合控件在底部添加了一个很大的边距,将其与其他组件分开。我尝试添加 Layer_bottomMargin = 0dp 没有成功,解决这个问题的唯一方法是添加 layer_height 一个固定值(即 15dp)而不是“wrap_content”,但是这个解决方案对我来说并不好,因为我想从我的化合物中删除一些组件控制。

我的复合控件的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left|top"
    android:clickable="false"
    android:background="#CCCCCC"
    android:layout_marginTop="@dimen/fieldset_margin"
    >
    <TextView
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_default"
        android:textSize="14sp"
        />
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@color/fieldset_lines_shadow"
        />
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@color/fieldset_lines_light"
        />
</LinearLayout>

我的层的代码包括这个复合控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#C8D5F6FF"
    android:gravity="top"
    android:layout_margin="8dp"
    >
    <com.apps.example.Header
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/fieldset_text_secondary"
        android:text="nananananan"
        android:textSize="10sp"
        android:layout_marginLeft="@dimen/fieldset_margin"
        />
</LinearLayout> 

抱歉,这是我的第一篇文章,我无法上传图片。

提前致谢。

4

1 回答 1

0

我通过覆盖复合控件类中的 onMeasure 方法部分解决了问题。我以前没有这样做,因为在官方文档中:

http://developer.android.com/guide/topics/ui/custom-components.html

表示在创建复合控件时不必重写此方法。

无论如何,这是部分解决方案,因为当化合物的单个控件以编程方式变为不可见 (GONE) 时,会发生与原始帖子中提到的相同问题但边距空间较小的问题。

我将继续尝试解决这个问题,但现在,这是我的解决方案。

谢谢!

于 2013-10-26T02:10:39.557 回答