33

我如何知道我的活动的可见大小?

我正在尝试获取 Activity 的实际大小,而不是 and 的高度和
宽度, 这给了我屏幕全尺寸。getHeight()getWidth()

4

8 回答 8

43

你会想看看Activity.getWindow()and Window.getDecorView(),并得到它的宽度/高度。

于 2010-06-17T10:14:54.337 回答
17

我更喜欢节省时间的答案:

myActivity.getWindow().getDecorView().getHeight();
myActivity.getWindow().getDecorView().getWidth();
于 2017-12-28T05:39:47.143 回答
15

我找到了一种方法来了解我的活动的确切高度:如果您有一个视图填充标题栏和任务栏后面的所有可用屏幕,只需使用 View.getBottom()来获取该活动的实际高度。

于 2010-06-18T10:06:44.403 回答
6

I did that using my root layout, the problem is that you can't obtain this info (width and height) in your onCreate/onStart/onResume methods when they are running by the first time.

Any moment after that, the size can be retrieved from your root layout (LinearLayout/FrameLayout/TableLayout/etc), in my case I was using FrameLayout:

FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getMeasuredWidth() );
Log.d(TAG, "height " + f.getMeasuredHeight() );

or:

FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getRight() );
Log.d(TAG, "height " + f.getBottom() );

Edited from here --------------------------------------------------------

I found a way to get this info on the onCreted method, but only if your have more then one activity. On my case I have a main activity that calls second activity.

On main_activity before I call the second_activity, by a new Itent, I can add extra info for the second_activity. Here is the code you need on the first activity:

Intent i = new Intent(this, SecondActivity.class);
i.putExtra("width", f.getMeasuredWidth());
i.putExtra("height", f.getMeasuredHeight());
startActivity(i);

After that on your second activity you can retrieve this info on the onCreate method of your second activity doing that:

int width = getIntent().getExtras().getInt("width");
int height = getIntent().getExtras().getInt("height");
于 2012-02-07T05:47:15.370 回答
3

有一种简单的方法可以使用getDefaultSize函数获得正确的视图大小。如果 View 跨越整个活动,您可以使用下一种方法来获取活动客户区域维度:

public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    int viewWidth = getDefaultSize(getWidth(), widthMeasureSpec); 
    int viewHeight = getDefaultSize(getHeight(), heightMeasureSpec);
    ...
}

要使 View 扩展到整个活动:

MyView testView = new MyView(this);
LinearLayout testViewGroup = new LinearLayout(this);
testView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                          LayoutParams.FILL_PARENT);
testViewGroup.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                               LayoutParams.FILL_PARENT);
testViewGroup.addView(testView);
setContentView(testViewGroup);
于 2012-12-02T06:30:39.123 回答
2

如果您使用的是 DataBinding(您应该使用),您可以调用binding.getRoot().getHeight()where bindingis your ViewDataBindingcreated withDataBindingUtil.setContentView()或其他此类DataBindingUtil方法。您需要等到布局具有大小,您可以这样做:

binding.getRoot().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        binding.getRoot().getViewTreeObserver().removeOnGlobalLayoutListener(this);

        int height = binding.getRoot().getHeight();
    }
});

请注意,您必须删除 OnGlobalLayoutListener ,否则每次布局中发生任何更改时它都会运行它。

于 2016-06-29T22:00:58.900 回答
1

在 onCreate()/onStart()/onResume() 方法中,您将得到高度和宽度为 0。原因是尚未设置大小。如果您想知道当前大小,最好在其中一种方法中使用观察者,并在获得当前大小时执行您想要的操作。

constraint_layout 是下面代码中的布局根元素:

ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_layout);
constraintLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout()  {
        constraintLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        int width = constraintLayout.getWidth();
        int height = constraintLayout.getHeight();
        doSmthMethod(width, height);
    }
});
于 2018-11-13T12:43:24.057 回答
1

获取活动的高度:

public static int getActivityHeight(Activity activity)
{
   return activity.findViewById(android.R.id.content).getHeight();
}

获取活动的宽度

public static int getActivityWidth(Activity activity)
{
    return activity.findViewById(android.R.id.content).getWidth();
}
于 2016-02-16T18:49:29.357 回答