版面宽度相等的高度 [英] Layout width equal height
问题描述
有没有一种方法,有不同的宽度,我的XML的高度?
我有一个线性布局一些按钮。按钮的宽度从设备依赖,因为他们水平填充屏幕。我想有方形按钮,这是我的问题。 有没有人能帮助我吗?
<按钮
机器人:ID =@ + ID / b_0xa
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:可点击=假
机器人:文本=@字符串/ b_0xa/>
您可以在您的Java code做到这一点很容易编程。
获取动态按钮的宽度[使用 INT X = mButton.getWidth()
],然后使用返回值来设置高度[ mButton.setHeight(X)
。
P.S。 :这是最好用的LayoutParams设置的任何视图的高度和宽度。而不是使用自动调用setHeight()因此,你应该使用setLayoutParams()。
mButton.setLayoutParams(新LinearLayout.LayoutParams(X,X));
Is there a method to have the height depending from the width in my xml?
I have some Buttons in a linear layout. The button's widths depending from the device because they fill the screen horizontally. I want to have square buttons and this is my problem. Is there one can help me?
<Button
android:id="@+id/b_0xa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:text="@string/b_0xa" />
You can do it easily programatically in your Java code.
Get the width of the button dynamically [using int x = mButton.getWidth()
], and then use the value returned to set the height [mButton.setHeight(x)
].
P.S. : It is advisable to use LayoutParams to set the height and width of any View. So, instead of using setHeight(), you should use setLayoutParams().
mButton.setLayoutParams(new LinearLayout.LayoutParams(x, x));
这篇关于版面宽度相等的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!