Android 如何获得活动的可见大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3060619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the visible size on an Activity?
提问by Tsimmi
How can I know the visible size of my activity?
我如何知道我的活动的可见大小?
I'm trying to get the Activity real size,
not the height and width from getHeight()
and getWidth()
,
which gives me the screen full size.
我正在尝试获取 Activity 的实际大小,
而不是来自getHeight()
and的高度和宽度getWidth()
,
这为我提供了全尺寸的屏幕。
采纳答案by Tsimmi
I've found a way to know the exact height of my activity: if you have a view filling all the available screen behind the title bar and the task bar, just use View.getBottom()
to get the real height of that activity.
我找到了一种方法来知道我的活动的确切高度:如果您有一个视图填充标题栏和任务栏后面的所有可用屏幕,只需使用 View.getBottom()
来获取该活动的实际高度。
回答by Dave
I thinkyou'll want to look at Activity.getWindow()
and Window.getDecorView()
, and get the width/height of that.
我想你会想看看Activity.getWindow()
and Window.getDecorView()
,并得到它的宽度/高度。
回答by Zon
I like time-saving answers more:
我更喜欢节省时间的答案:
myActivity.getWindow().getDecorView().getHeight();
myActivity.getWindow().getDecorView().getWidth();
回答by Derzu
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.
我使用我的根布局做到了这一点,问题是当它们第一次运行时,您无法在 onCreate/onStart/onResume 方法中获取此信息(宽度和高度)。
Any moment after that, the size can be retrieved from your root layout (LinearLayout/FrameLayout/TableLayout/etc), in my case I was using FrameLayout:
之后的任何时刻,都可以从您的根布局(LinearLayout/FrameLayout/TableLayout/etc)中检索大小,在我的情况下,我使用的是 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.
我找到了一种在 onCreted 方法上获取此信息的方法,但前提是您有多个活动。就我而言,我有一个主要活动称为第二个活动。
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:
在调用 second_activity 之前的 main_activity,通过一个新的 Itent,我可以为 second_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:
之后,在您的第二个活动中,您可以在第二个活动的 onCreate 方法中检索此信息:
int width = getIntent().getExtras().getInt("width");
int height = getIntent().getExtras().getInt("height");
回答by Weaknespase
There is simple way to get right View size using getDefaultSize
function. In case of View spanning on entire activity, you can use next approach to get activity client region dimensions:
有使用getDefaultSize
函数获得正确视图大小的简单方法。如果视图跨越整个活动,您可以使用下一种方法来获取活动客户区域维度:
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int viewWidth = getDefaultSize(getWidth(), widthMeasureSpec);
int viewHeight = getDefaultSize(getHeight(), heightMeasureSpec);
...
}
To make View extending on entire activity:
要使视图扩展到整个活动:
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);
回答by LukeWaggoner
If you are using DataBinding (which you should be), you can call binding.getRoot().getHeight()
where binding
is your ViewDataBinding
created with DataBindingUtil.setContentView()
or other such DataBindingUtil
method. You'll need to wait until the layout has size, which you can do like this:
如果您正在使用 DataBinding(您应该使用),您可以调用binding.getRoot().getHeight()
where binding
is your ViewDataBinding
created withDataBindingUtil.setContentView()
或其他此类DataBindingUtil
方法。您需要等到布局具有大小,您可以这样做:
binding.getRoot().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
binding.getRoot().getViewTreeObserver().removeOnGlobalLayoutListener(this);
int height = binding.getRoot().getHeight();
}
});
Note that you have to remove the OnGlobalLayoutListener or else it'll be running this every time anything changes in your layout.
请注意,您必须删除 OnGlobalLayoutListener 否则每次布局发生任何变化时它都会运行它。
回答by Hans
Get the height of the activity:
获取活动的高度:
public static int getActivityHeight(Activity activity)
{
return activity.findViewById(android.R.id.content).getHeight();
}
Get the width of the activity
获取活动的宽度
public static int getActivityWidth(Activity activity)
{
return activity.findViewById(android.R.id.content).getWidth();
}
回答by Serg.Stankov
You will get height and width as 0 at onCreate()/onStart()/onResume() methods. Reason is size is not set yet. If you want to know current size you'd better use observer in one of this methods and do what you want when you get current size.
您将在 onCreate()/onStart()/onResume() 方法中获得高度和宽度为 0。原因是大小尚未设置。如果您想知道当前大小,最好在其中一种方法中使用观察者,并在获得当前大小时执行您想要的操作。
constraint_layout is layout root element in code bellow:
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);
}
});