【问题标题】:Custom component layout order自定义组件布局顺序
【发布时间】:2013-01-11 11:52:19
【问题描述】:

我有一个自定义组件覆盖 onLayout 方法,如下所示:

@Override
    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int[] location = new int[2];
        this.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        int w = this.getWidth();
        int h = y+(8*CELL_HEIGHT);

        updateMonthName();
        initCells();

        CELL_MARGIN_LEFT = 0;
        CELL_MARGIN_TOP = monthNameBounds.height();
        CELL_WIDTH = w / 7;

        setFrame(x, y, x+w, h);
        super.onLayout(true, x, y, w, h);
    }

但是,在线性布局中使用该组件时如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="10dp"
    android:orientation="vertical">

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView above"
        android:textSize="15sp"
        />

    <com.project.calenderview.CalendarView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView below"
        android:textSize="15sp"
        />

</LinearLayout>

布局被渲染使得文本视图“TextView above”是第一个,“TextView below”是第二个,然后我的组件应该在两者之间。

编辑: 这里也是 onDraw:

@Override
    protected void onDraw(Canvas canvas) {
        // draw background
        super.onDraw(canvas);

        //Draw month name
        canvas.drawText(monthName, (this.getWidth() / 2) - (monthNameBounds.width() / 2), monthNameBounds.height() + 10, monthNamePaint);

        //Draw arrows
        monthLeft.draw(canvas);
        monthRight.draw(canvas);

        // draw cells
        for(Cell[] week : mCells) {
            for(Cell day : week) {
                if(day == null) continue;
                day.draw(canvas);   
            }
        }

    }

这里做错了什么?

谢谢

【问题讨论】:

    标签: java android android-layout android-custom-view


    【解决方案1】:

    onLayout 方法传递 int left, int top, int right, int bottom 指示您的视图应该在哪里绘制,但显然您完全忽略了它。您可以使用这些值来正确布局。

    编辑:

    如果您不扩展 ViewGroup,则不应覆盖 onLayout(bool, int, int, int, int)

    来自文档View.onLayout

    当此视图应分配大小和位置时从布局调用 它的每个孩子。有孩子的派生类应该覆盖 这个方法并在他们的每个孩子上调用布局。

    带有孩子的派生类应该重写这个方法并在他们的每个孩子上调用布局

    您对最终结果的问题出在代码的其他部分。

    【讨论】:

    • 顶部和底部的值完全相同 = 47,这意味着高度为零......这就是我决定计算自己的值的原因。有什么理由吗?
    • 您的自定义组件是扩展 ViewGroup 还是只是 View ?
    • 我正在扩展 ImageView 以更具体
    • 好的,通过不覆盖这个方法,我的组件的高度仍然是0。这里有什么问题?我可以通过使用带有计算值的 ImageView 的 setFrame 来克服这个问题的唯一方法。有没有其他选择?
    • 你能展示你的 onDraw(Canvas) 的代码吗?如果它不是太大,或者至少是一些相关的sn-p
    猜你喜欢
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    相关资源
    最近更新 更多