设置视图的高度,以广场做它在仅知道它的宽度重量 [英] Set the Height of View to Make It Square By Knowing Only Its Width Weight
问题描述
我有这样的场景:
一个水平LinearLayout中填充的容器和 weightSum = 100
,和内部两种意见与重量的50只。
现在我怎么做平方米这两种观点(如高度必须等于它们的宽度)。中的LinearLayout行数是未知的,所以基本上,我不能将它们包装在一个垂直容器的重量在这种情况下。
现在我怎么做这两种观点方(如高度必须 等于其宽度)。
如果您在短短的这两个观点的的LinearLayout
你有两个选择:
-
请勿直接设置您的布局文件的内容视图,而不是抬高它,所以你必须从该文件中引用的根源。然后发布
的Runnable
上的根视图,计算出所需的高度,将其设置回各自的两个孩子的的LinearLayout
,它包装他们:最终视图V = getLayoutInflater()。膨胀( R.layout.views_specialheight,NULL); v.post(新的Runnable(){ @覆盖 公共无效的run(){ setupHeight((ViewGroup中)V); } }); 的setContentView(五);
其中,
setupHeight()
的方法:私人无效setupHeight(ViewGroup中VG){ 诠释计数= vg.getChildCount(); 的for(int i = 0; I<计数;我++){ 最终的视图V = vg.getChildAt(我); 如果(V的instanceof的LinearLayout){ INT宽度= v.getWidth(); LinearLayout.LayoutParams LP; 图1 =((的LinearLayout)ⅴ).getChildAt(0); LP =(LinearLayout.LayoutParams)one.getLayoutParams(); lp.height =宽度/ 2; one.setLayoutParams(LP); 景两=((LinearLayout中)V).getChildAt(1); LP =(LinearLayout.LayoutParams)two.getLayoutParams(); lp.height =宽度/ 2; two.setLayoutParams(LP); } } }
此方法将工作pretty的好,如果你只是有一个包装
的ViewGroup
的子类,它包装的的LinearLayout
行。它可以(也应该)得到改善,但我敢肯定,你明白了吧。 -
第二个选择是使用自定义的
的ViewGroup
(这可能取决于你是刨做的的LinearLayout使用
)而不是的LinearLayout
是这样的:< com.luksprog.oat.views.SpecialHeightViewGroup 机器人:layout_width =match_parent 机器人:layout_height =WRAP_CONTENT> <按钮 机器人:ID =@ + ID /按钮1 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:后台=#0077cc 机器人:文本=按钮/> <按钮 机器人:ID =@ + ID /按钮2 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:后台=#99CC00 机器人:文本=按钮/> < /com.luksprog.oat.views.SpecialHeightViewGroup>
在 SpecialHeightViewGroup
是一类这样的:
类SpecialHeightViewGroup扩展的ViewGroup {
公共SpecialHeightViewGroup(上下文的背景下,ATTRS的AttributeSet,
INT defStyle){
超(背景下,ATTRS,defStyle);
}
公共SpecialHeightViewGroup(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
}
公共SpecialHeightViewGroup(上下文的背景下){
超(上下文);
}
@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
INT widthSize = MeasureSpec.getSize(widthMeasureSpec);
INT widthMode = MeasureSpec.getMode(widthMeasureSpec);
如果(widthMode == MeasureSpec.EXACTLY
|| widthMode == MeasureSpec.AT_MOST){
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(
widthSize / 2,MeasureSpec.EXACTLY));
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(
widthSize / 2,MeasureSpec.EXACTLY));
setMeasuredDimension(widthSize,widthSize / 2);
} 其他 {
widthSize = 800; //我们没有限制,使之大如我们想要
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(
widthSize / 2,MeasureSpec.EXACTLY));
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(
widthSize / 2,MeasureSpec.EXACTLY));
setMeasuredDimension(widthSize,400);
}
}
@覆盖
保护无效onLayout(布尔改变,诠释L,INT T,INT R,int b)在{
观看某一= getChildAt(0);
one.layout(0,0,的getWidth()/ 2,的getHeight());
景两= getChildAt(1);
two.layout(的getWidth()/ 2,0,的getWidth(),的getHeight());
}
}
自定义类是不正确的测试,所以它可能有错误(这只是一个例子)。
Offtopic:你们是不是由任何机会,以创建像在新的Windows手机的UI(与瓷砖的东西)
I have this scenario:
A horizontal LinearLayout that fills the container and weightSum=100
, and two views inside with weight of 50 each.
Now how do I make these two views square (e.g. the height must be equal to their width). The number of LinearLayout rows is unknown, so basically, I can not wrap them in a vertical container with weights in this case.
Now how do I make these two views square (e.g. the height must be equal to their width).
If you have just those two views in the LinearLayout
you have two options:
Don't set your layout file directly as the content view, instead inflate it so you have a reference to the root from that file. Then post a
Runnable
on that root view, calculate the desired height and set it back to each of the two children of theLinearLayout
that wraps them:final View v = getLayoutInflater().inflate( R.layout.views_specialheight, null); v.post(new Runnable() { @Override public void run() { setupHeight((ViewGroup) v); } }); setContentView(v);
where
setupHeight()
is the method:private void setupHeight(ViewGroup vg) { int count = vg.getChildCount(); for (int i = 0; i < count; i++) { final View v = vg.getChildAt(i); if (v instanceof LinearLayout) { int width = v.getWidth(); LinearLayout.LayoutParams lp; View one = ((LinearLayout) v).getChildAt(0); lp = (LinearLayout.LayoutParams) one.getLayoutParams(); lp.height = width / 2; one.setLayoutParams(lp); View two = ((LinearLayout) v).getChildAt(1); lp = (LinearLayout.LayoutParams) two.getLayoutParams(); lp.height = width / 2; two.setLayoutParams(lp); } } }
This method will work pretty well if you just have a wrapper
ViewGroup
subclass that wraps thoseLinearLayout
rows. It can(and should) be improved, but I'm sure you get the point.The second option is to use a custom
ViewGroup
(this may be used depending on what you were planing to do with theLinearLayout
) instead of theLinearLayout
like this:<com.luksprog.oat.views.SpecialHeightViewGroup android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#0077cc" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#99cc00" android:text="Button" /> </com.luksprog.oat.views.SpecialHeightViewGroup>
The SpecialHeightViewGroup
is a class like this:
class SpecialHeightViewGroup extends ViewGroup {
public SpecialHeightViewGroup(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public SpecialHeightViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SpecialHeightViewGroup(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY
|| widthMode == MeasureSpec.AT_MOST) {
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
widthSize / 2, MeasureSpec.EXACTLY));
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
widthSize / 2, MeasureSpec.EXACTLY));
setMeasuredDimension(widthSize, widthSize / 2);
} else {
widthSize = 800; // we don't have restrictions, make it as big as we want
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
widthSize / 2, MeasureSpec.EXACTLY));
measureChildren(MeasureSpec.makeMeasureSpec(widthSize / 2,
MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
widthSize / 2, MeasureSpec.EXACTLY));
setMeasuredDimension(widthSize, 400);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
View one = getChildAt(0);
one.layout(0, 0, getWidth() / 2, getHeight());
View two = getChildAt(1);
two.layout(getWidth() / 2, 0, getWidth(), getHeight());
}
}
The custom class isn't properly tested so it may have bugs(it's just an example).
Offtopic: Are you trying by any chance to create an UI like on the new Windows phones(with the tiles thing)?
这篇关于设置视图的高度,以广场做它在仅知道它的宽度重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!