正确的方法覆盖固定大小的自定义视图 [英] Proper way override fixed size custom view

查看:122
本文介绍了正确的方法覆盖固定大小的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个黄色背景自定义视图。我打算添加一个红色背景的TextView ,是 match_parent 的宽度和高度就可以了。下面是我做了什么。

I have a custom view with Yellow background. I plan to add a Red background TextView, with match_parent for both width and height on it. Here's what I had done.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout mainView = (LinearLayout)this.findViewById(R.id.screen_main);
        RateAppBanner rateAppBanner = new RateAppBanner(this);
        mainView.addView(rateAppBanner);
    }
}

RateAppBanner.java

public class RateAppBanner extends LinearLayout {

    public RateAppBanner(Context context) {
        super(context);

        setOrientation(HORIZONTAL);

        LayoutInflater.from(context).inflate(R.layout.rate_app_banner, this, true);

        this.setBackgroundColor(Color.YELLOW);
    }
}

rate_app_banner.xml

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

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#ffffffff"
        android:background="#ffff0000"
        android:text="Hello World" />

</LinearLayout>

现在,我想有一个固定的宽度和放大器;高度自定义视图。我意识到,我有固定的宽度和放后;高度自定义视图,增加的TextView 不服从 match_parent 属性。

Now, I would like to have a fixed width & height custom view. I realize, after I'm having fixed width & height custom view, the added TextView doesn't obey match_parent attribute.

下面是我的自定义视图所做的更改。

Here's the change I had done on custom view.

public class RateAppBanner extends LinearLayout {
    ...

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int desiredWidth = 320;
        int desiredHeight = 50;

        desiredWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, desiredWidth, getResources().getDisplayMetrics());
        desiredHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, desiredHeight, getResources().getDisplayMetrics());

        super.onMeasure(desiredWidth, desiredHeight);
        //MUST CALL THIS
        setMeasuredDimension(desiredWidth, desiredHeight);
    }

我认识的增加的TextView match_parent 了!

现在,我们可以看到黄色的自定义视图是有固定的尺寸为320x50。我希望红的TextView 将整个自定义视图填补因 match_parent 属性。

Now, we can see the Yellow custom view is having fixed size 320x50. I expect the Red TextView will fill up the entire custom view due to match_parent attribute.

然而,事实并非如此。我相信我执行自定义视图的 onMeasure 是不正确的。我想知道是什么来解决这个问题的正确方法是什么?

However, that's not the case. I believe my implementation for custom view's onMeasure isn't correct. May I know what is the correct way to fix this?

完整的源$ C ​​$ C可从的 abc.zip

The complete source code can be downloaded from abc.zip

推荐答案

经过大量的试验和错误和做研究工作,最终找到答案。

After lots of trial and error and doing research work, final found answer.

您已经为布局的测量,但不适合孩子看,所以你需要把这个onMeasure方法,

You have set measurements for layout but not for child view, so for that you need to put this in onMeasure method,

        super.onMeasure(
            MeasureSpec.makeMeasureSpec(desiredWidth, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(desiredHeight, MeasureSpec.EXACTLY));

参考链接:<一个href=\"http://stackoverflow.com/questions/13394181/inflated-children-of-custom-linearlayout-dont-show-when-overriding-onmeasure\">Inflated定制的LinearLayout儿童重写时,不显示onMeasure

和最后它的工作:)

这篇关于正确的方法覆盖固定大小的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆