【问题标题】:Different values of width returned in fragment片段中返回的不同宽度值
【发布时间】:2016-02-11 22:07:20
【问题描述】:

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

如果在onViewCreated 里面我使用:

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

但如果我使用:

 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() 那么有什么办法可以解决这个问题吗?

 <?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>

【问题讨论】:

  • 向我们展示定义ID为R.id.background的RelativeLayout的布局文件。
  • 我刚刚更新了布局
  • 如果它是你想要的,为什么不直接使用 DisplayMetrics 呢?
  • 如果我使用显示指标,即使我在实际绘制背景颜色时得到这些值,它也只显示在屏幕的 90% 上(宽度,高度都可以)
  • 等等,match_parent 应该总是有效的。我以为你对实际值感兴趣。

标签: android android-fragments


【解决方案1】:

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

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();

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

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();

【讨论】:

  • 我现在试试,然后回复你:D
  • 我恢复了高度 - 16777215 宽度 - 16777215
  • 我必须转换它们还是什么?为什么他们是平等的?
  • 没有。 background.getMeasuredHeight() 应该返回正确的像素高度。我还用一个返回正确像素值的示例更新了答案。就我而言,我使用了ViewGroup.LayoutParams.WRAP_CONTENT。也许.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) 以这种方式使用时不会返回正确的值。
  • 我更改了它以匹配您的编辑,现在我得到 16777214 - 16777214
【解决方案2】:

像下面这样使用 ViewTreeObserver:

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);
    } 
} 
}); 

【讨论】:

  • 我希望找到 displayMetrics 的解决方案。如果我想做这样的事情,我可以继续使用background.post 方法
猜你喜欢
  • 1970-01-01
  • 2021-04-24
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
相关资源
最近更新 更多