Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在布局之前获得布局大小';展示什么?_Android_View - Fatal编程技术网

Android 如何在布局之前获得布局大小';展示什么?

Android 如何在布局之前获得布局大小';展示什么?,android,view,Android,View,我对包含图像视图和相对视图的布局进行充气。布局的宽度设置为match\u parent,高度设置为wrap\u content。高度由图像视图确定,但设置为图像视图的图像是从internet动态加载的 因为我知道图像比率,所以我想在显示ImageView之前设置它的大小,以避免在设置图像时由于布局高度的变化而在UI中跳转 要设置大小,我需要布局宽度,要计算ImageView高度 我试过了 int width = header.getMeasuredWidth(); 但由于布局尚未绘制,因此返回

我对包含
图像视图
相对视图
的布局进行充气。布局的宽度设置为
match\u parent
,高度设置为
wrap\u content
。高度由
图像视图
确定,但设置为
图像视图
的图像是从internet动态加载的

因为我知道图像比率,所以我想在显示
ImageView
之前设置它的大小,以避免在设置图像时由于布局高度的变化而在UI中跳转

要设置大小,我需要布局宽度,要计算
ImageView
高度

我试过了

int width = header.getMeasuredWidth();
但由于布局尚未绘制,因此返回0

正如建议的那样,我以前也尝试过使用
度量值

但是
measure
返回
NullPointerException

我该怎么做

list_header.xml


您可以在Activity的onWindowFocusChanged(boolean hasFocus)方法中获取大小。

您可以使用ViewTreeObserver进行尝试。 您的片段/活动应该在OnActivityCreate声明处理程序中实现OnGlobalLayoutListener

ViewGroup container = (ViewGroup) findViewById(R.id.header_text_container)
ViewTreeObserver vto = container.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(this);
当大小可用时,将宽度逻辑放入侦听器

@Override
public void onGlobalLayout() {
            // Remember to remove handler
    container.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            View header = getActivity().getLayoutInflater().inflate(R.layout.list_header, null);

            int width = header.getMeasuredWidth(); // 0 because not drawn yet
            ...
}

您可以在onGlobalLayout中获取视图的宽度和高度,这是一种回调方法,当全局布局状态或视图树中视图的可见性发生更改时(即在绘制视图和已知大小后)调用该方法


你根本不能。您可以尝试从视图组覆盖onMeasure
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mListView = (ListView) getView().findViewById(R.id.listview);

    View header = getActivity().getLayoutInflater().inflate(R.layout.list_header, null);

    int width = header.getMeasuredWidth(); // 0 because not drawn yet

    ImageView pic = (ImageView) header.findViewById(R.id.pic);
    pic.setLayoutParams(new LayoutParams(width, (int) (width/imgRatio)));

    pic.invalidate();
    header.invalidate();

    /* ... */
}
ViewGroup container = (ViewGroup) findViewById(R.id.header_text_container)
ViewTreeObserver vto = container.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(this);
@Override
public void onGlobalLayout() {
            // Remember to remove handler
    container.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            View header = getActivity().getLayoutInflater().inflate(R.layout.list_header, null);

            int width = header.getMeasuredWidth(); // 0 because not drawn yet
            ...
}
ViewTreeObserver vto = header.getViewTreeObserver();

if(vto!=null){
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            header.getWidth();
            header.getHeight();
        }
    });
}