首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >fragment中返回的宽度值不同

fragment中返回的宽度值不同
EN

Stack Overflow用户
提问于 2016-02-12 06:07:21
回答 2查看 163关注 0票数 0

我正在尝试确定屏幕的宽度和高度,以便在片段中使用它们。我的片段的布局具有id background

如果在onViewCreated中我使用:

代码语言:javascript
运行
复制
background = (RelativeLayout) view.findViewById(R.id.background);
        background.post(new Runnable() {
            @Override
            public void run() {
                int width = background.getMeasuredWidth();
                int height = background.getMeasuredHeight();
                Log.d("askj", width + "+" + height);
            }
        });

我得到了1440+2276

但是如果我使用:

代码语言:javascript
运行
复制
 WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics displayMetrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);
    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;
    Log.d("askj", width + "+" + height);

或者我得到的其他相关方法:

1440+2560

我使用这些参数来设置背景,我可以清楚地看到,如果我使用第二种方法,整个屏幕尺寸都不会被占用。我真的不想使用那个Runnable(),所以有什么方法可以解决这个问题吗?

代码语言:javascript
运行
复制
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/background"
    tools:context="com.example.home.background.HomeScreen">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/img"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:textSize="22sp"
        />
</RelativeLayout>
EN

回答 2

Stack Overflow用户

发布于 2016-02-12 18:14:33

在activity将背景视图添加到视图层次结构之前,您可以使用measure方法测量背景视图。

代码语言:javascript
运行
复制
background = (RelativeLayout) view.findViewById(R.id.background);
background.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
int backgroundHeight = background.getMeasuredHeight();
int backgroundWidth = background.getMeasuredWidth();

在我的例子中,我测量了一个膨胀的视图,但它的宽度和高度设置为match_paent

代码语言:javascript
运行
复制
View view = inflater.inflate(R.layout.table_field_text, null, false);
view.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int viewHeight = view.getMeasuredHeight();
票数 0
EN

Stack Overflow用户

发布于 2016-02-12 21:33:40

使用ViewTreeObserver,如下所示:

代码语言:javascript
运行
复制
background = (RelativeLayout) view.findViewById(R.id.background);

final ViewTreeObserver vto = background.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
@SuppressWarnings("deprecation") 
@Override 
public void onGlobalLayout() { 
    //get View Width and height here 
    int width = background.getWidth();
    int height = background.getHeight();

    //Call anyfunction which uses width and height here
    function(width,height);

    //Then remove layoutChange Listener 
    ViewTreeObserver vto = background.getViewTreeObserver();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        vto.removeOnGlobalLayoutListener(this);
    } else { 
        vto.removeGlobalOnLayoutListener(this);
    } 
} 
}); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35351028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档