Android 自定义组件中奇怪组件的视图

Android 自定义组件中奇怪组件的视图,android,android-layout,Android,Android Layout,我创建了从ViewGroup扩展的自定义组件。我使用onDraw作为背景,使用onLayout作为定位视图 但是在放大布局和定位视图之后,这些视图看起来很奇怪。EditText的提示是垂直的,按钮的文本不居中。但当输入文本时,会像往常一样出现。如图所示: 源代码: public class CustomView extends LinearLayout private Bitmap fullImage; private int canvasWidth; private i

我创建了从ViewGroup扩展的自定义组件。我使用onDraw作为背景,使用onLayout作为定位视图

但是在放大布局和定位视图之后,这些视图看起来很奇怪。EditText的提示是垂直的,按钮的文本不居中。但当输入文本时,会像往常一样出现。如图所示:

源代码:

public class CustomView extends LinearLayout
    private Bitmap fullImage;
    private int canvasWidth;
    private int canvasHeight;

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
        initializeCanvasSize(context);
        final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.favorites_layout, this, true);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final ViewGroup parentLayout = (ViewGroup) getChildAt(0);
        parentLayout.layout(l, t, r, b);
        final int elementCenter = canvasHeight / 3; // EditText, EditText and Button
        final View name = parentLayout.getChildAt(NAME_ELEMENT);
        final View url = parentLayout.getChildAt(URL_ELEMENT);
        final View save = parentLayout.getChildAt(SAVE_BUTTON_ELEMENT);
        name.layout(10, elementCenter / 2 - EDIT_TEXT_HEIGHT / 2, canvasWidth - 10, elementCenter / 2 + EDIT_TEXT_HEIGHT / 2);
        url.layout(10, elementCenter * 2 - elementCenter / 2 - EDIT_TEXT_HEIGHT / 2, canvasWidth - 10, elementCenter * 2 - elementCenter / 2 + EDIT_TEXT_HEIGHT / 2);
        save.layout(canvasWidth - 220, elementCenter * 3 - elementCenter / 2 - BUTTON_HEIGHT / 2, canvasWidth - 10, elementCenter * 3 - elementCenter / 2 + BUTTON_HEIGHT / 2);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int desiredWSpec = MeasureSpec.makeMeasureSpec(canvasWidth, MeasureSpec.AT_MOST);
        final int desiredHSpec = MeasureSpec.makeMeasureSpec(canvasHeight, MeasureSpec.AT_MOST);
        this.setMeasuredDimension(desiredWSpec, desiredHSpec);
    }

    @Override
    protected void onDraw(Canvas cvs) {
        if (fullImage == null) {
            fullImage = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
            final Canvas canvas = new Canvas(fullImage);
            paint.reset();
            paint.setColor(Color.parseColor("#CC000000"));
            canvas.drawRect(0, 0, canvasWidth, canvasHeight, paint);
        }
        cvs.drawBitmap(fullImage, 0, 0, null);
    }

}
收藏夹\u layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:background="@drawable/search_url_selector"
        android:singleLine="true"
        android:inputType="text"
        android:paddingLeft="5dp"
        android:hint="@string/favorite_name"/>

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:background="@drawable/search_url_selector"
        android:singleLine="true"
        android:inputType="text"
        android:paddingLeft="5dp"
        android:hint="@string/favorite_url"/>

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:background="@drawable/save_favorites_button_selector"
        android:text="@string/favorite_save"/>

</FrameLayout>
我想这可能是因为选择器错了。但当我移除它时,结果是一样的


请您建议我至少在哪个区域检查哪些内容才能找到正确答案?

在测量中测量所有子对象。另外,您不需要充气器。充气,您可以直接在自定义视图组组件中以xml格式添加子视图,无需额外的FrameLayout是否有效?我指的是测量和移除框架布局是的,它有效。我给孩子们量了尺寸,但不是很好看。我还删除了FrameLayout,并使用组件直接在xml中插入内容。很好!谢谢