定制的LinearLayout的充气儿童重写onMeasure时不显示 [英] Inflated children of custom LinearLayout don't show when overriding onMeasure

查看:139
本文介绍了定制的LinearLayout的充气儿童重写onMeasure时不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图表现出 MyCustomLinearLayout 延伸的LinearLayout 。我充气 MyCustomLinearLayout 的android:layout_height =match_parent属性。
我要的是表现出的ImageView 在这个 MyCustomLinearLayout 。这个的ImageView 的高度应为 match_parent 和宽度应该等于高度。我试图通过覆盖 onMeasure()的方法来做到这一点。会发生什么,就是 MyCustomLinearLayout 确实成为方形像它应该,但的ImageView 不显示。

I'm trying to show a MyCustomLinearLayout which extends LinearLayout. I'm inflating the MyCustomLinearLayout with the android:layout_height="match_parent" attribute. What I want is to show an ImageView in this MyCustomLinearLayout. The height of this ImageView should be match_parent, and the width should be equal to the height. I'm trying to accomplish this by overriding the onMeasure() method. What happens, is that MyCustomLinearLayout does become square like it should, but the ImageView is not showing.

下面我用code。的请注意,这是我的问题的一个非常简化版本。在的ImageView 将被更复杂的组件更换。

Below my used code. Please note that this is an extremely simplified version of my problem. The ImageView will be replaced by a more complex component.

public MyCustomLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

private void init(Context context) {
    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.view_myimageview, this);

    setBackgroundColor(getResources().getColor(R.color.blue));
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    setMeasuredDimension(height, height);
}

view_myimageview.xml 文件:

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

    <ImageView
        android:id="@+id/view_myimageview_imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />

</merge>

所以,当我重写 onMeasure()法,的ImageView 没有显示,当我做不覆盖 onMeasure()法,的ImageView 节目,但方式太少,因为 MyCustomLinearLayout 的宽度太小。

So when I override the onMeasure() method, the ImageView is not showing, when I don't override the onMeasure() method, the ImageView shows, but way too small, because the MyCustomLinearLayout's width is too small.

推荐答案

这不是你如何重写尤其是默认的SDK布局 onMeasure 方法。现在你的code你刚才提出的 MyCustomLinearLayout 平方分配给它一定的价值。但是,你没有衡量其子,使他们没有尺寸,并不会出现在屏幕上。

That's not how you override the onMeasure method especially of a default SDK layout. Right now with your code you just made the MyCustomLinearLayout square assigning it a certain value. But, you didn't measured its children so they are sizeless and don't appear on the screen.

我不知道这会工作,但尝试这个办法:

I'm not sure this would work but try this:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int size = getMeasuredHeight();
    super.onMeasure(MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY));
}

当然,这将基本上做到 onMeasure 工作两次,但在的ImageView 应该是现在可见的填充它的父。还有其他的解决方案,但你的问题是细节上有点少。

Of course this will basically do the work of onMeasure twice but the ImageView should be now visible filling it's parent. There are other solutions but your question is a bit scarce on details.

这篇关于定制的LinearLayout的充气儿童重写onMeasure时不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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