Android layout 在运行时获取布局高度和宽度
如何获取在xml中定义为填充父级的线性布局的宽度和高度?我尝试过测量方法,但我不知道为什么它不能给出准确的值。在oncreate方法完成之前,我需要在活动中使用这些值 宽度和高度值是在创建布局后设置的,放置元素后,将对其进行测量。在第一次调用onSizeChanged时,参数将为0,因此如果您使用该检查 这里再详细一点 这里呢 以下是如何使用onLayout:Android layout 在运行时获取布局高度和宽度,android-layout,Android Layout,如何获取在xml中定义为填充父级的线性布局的宽度和高度?我尝试过测量方法,但我不知道为什么它不能给出准确的值。在oncreate方法完成之前,我需要在活动中使用这些值 宽度和高度值是在创建布局后设置的,放置元素后,将对其进行测量。在第一次调用onSizeChanged时,参数将为0,因此如果您使用该检查 这里再详细一点 这里呢 以下是如何使用onLayout: @Override protected void onLayout(boolean changed, int l, int t, in
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = someView.getWidth();
int height = someView.getHeight();
}
假设我必须获得XML中定义的
LinearLayout
宽度。我必须通过XML获取它的引用。将LinearLayout
l
定义为实例
l = (LinearLayout)findviewbyid(R.id.l1);
ViewTreeObserver observer = l.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
init();
l.getViewTreeObserver().removeGlobalOnLayoutListener(
this);
}
});
protected void init() {
int a= l.getHeight();
int b = l.getWidth();
Toast.makeText(getActivity,""+a+" "+b,3000).show();
}
callfragment();
}
您可以将布局更改侦听器添加到布局中,以获得最新的高度和宽度,甚至是上次更改之前的高度和宽度 在API级别11中添加 添加一个侦听器,该侦听器将在视图边界更改时调用 由于布局处理
要使其正常工作,您需要检查所需的高度值是否大于0,然后首先删除onGlobalLayout侦听器,并根据需要对高度执行任何操作。侦听器连续调用其方法,并且在第一次调用时,无法保证视图得到正确测量
final LinearLayout parent = (LinearLayout) findViewById(R.id.parentView);
parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int availableHeight = parent.getMeasuredHeight();
if(availableHeight>0) {
parent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//save height here and do whatever you want with it
}
}
});
基于MGDroid对API 16+的回答,使用Kotlin的通用方法
/**
* Align height of a container from wrap-content to actual height at runtime.
* */
private fun <T: ViewGroup> alignContainerHeight(container: T) {
container.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
// Obtain runtime height
val availableHeight = container.measuredHeight
if (availableHeight > 0) {
container.viewTreeObserver.removeOnGlobalLayoutListener(this)
setContainerHeight(container, availableHeight)
}
}
})
}
/**
* Note: Assumes that the parent is a LinearLayout.
* */
private fun <T : ViewGroup> setContainerHeight(container: T, availableHeight: Int) {
val availableWidth = container.measuredWidth
val params = LinearLayout.LayoutParams(availableWidth, availableHeight)
// Note: getLayoutParams() returns null if no parent exists
if (container.layoutParams != null) {
container.layoutParams = params
}
}
/**
*运行时,将容器的高度从包裹内容对齐到实际高度。
* */
私人娱乐集装箱八号(集装箱:T){
container.viewTreeObserver.addOnGlobalLayoutListener(对象:viewTreeObserver.OnGlobalLayoutListener{
覆盖Loballayout(){
//获取运行时高度
val availableHeight=容器的测量高度
如果(可用高度>0){
container.viewTreeObserver.removeOnGlobalLayoutListener(此)
setContainerHeight(容器,可用高度)
}
}
})
}
/**
*注意:假设父对象是线性布局。
* */
私人娱乐设置容器重量(容器:T,可用重量:Int){
val availableWidth=容器.measuredWidth
val params=LinearLayout.LayoutParams(可用宽度、可用高度)
//注意:如果不存在父级,getLayoutParams()将返回null
if(container.layoutParams!=null){
container.layoutParams=参数
}
}
它在我的项目中起作用:
DisplayMetrics displaymetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
IIRC在测量之前,你无法得到任何物体的高度/宽度。在调用onSizeChanged时已指定,如果仅替代布局,则所有视图都应具有高度和宽度width@tom502你能给我一个链接或一段代码吗?这将非常有帮助。请注意!在onCreate完成之前,它不会为您提供值。我曾经在onCreate方法中调用过它,但它的覆盖方法将很晚调用。看起来你的应用程序运行得很慢,但它解决了我的问题。这就成功了-谢谢。API级别16不推荐使用remove listener调用。这篇文章描述了如何支持对早期和后期API的调用,因为某些原因,在方向更改后不会调用它
DisplayMetrics displaymetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;