问题描述
我试图设置布局为FILL_PARENT,而具有视图的高度一样的长度,使布局的方形的宽度。
I am trying to set the width of the layout to "fill_parent" while having the height of the view just the same length, to make the layout a square.
任何建议将AP preciate,在此先感谢! :D
Any suggestions will be appreciate, thanks in advance! :D
推荐答案
您可以尝试最初设定layout_height到WRAP_CONTENT。但是,从那里,我认为你必须去到code。只是为了实验,在您的活动尝试是这样的:
You might try initially setting the layout_height to wrap_content. But from there I think you have to go into code. Just to experiment, in your activity try something like:
@Override
public void onResume(){
super.onResume();
findViewById(R.id.squareView).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
View squareView = findViewById(R.id.squareView);
LayoutParams layout = squareView.getLayoutParams();
layout.height = squareView.getWidth();
squareView.setLayoutParams(layout);
squareView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
在哪里R.id.squareView是有问题的视图的ID。并注意,这code被包裹在onGlobalLayout呼吁,以确保squareView.getWidth()有一个有意义的值。
Where R.id.squareView is the id of the view in question. And note, this code is wrapped in the onGlobalLayout call to ensure that squareView.getWidth() has a meaningful value.
这篇关于如何设置固定纵横比为Android的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!