Android中设置Button的宽度
如何为 Android 按钮设置固定宽度?每次我尝试设置固定宽度时,它都会填充当前父级(RelativeView)。这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativelayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:layout_height="wrap_content" android:editable="false" android:layout_width="fill_parent" android:id="@+id/output"></EditText>
<Button android:layout_height="wrap_content" android:id="@+id/Button01" android:layout_below="@id/output" android:text="7" android:layout_width="wrap_content"></Button>
<Button android:layout_below="@id/output" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button02" android:layout_toRightOf="@+id/Button01" android:text="8"></Button>
<Button android:layout_below="@id/output" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button03" android:layout_toRightOf="@+id/Button02" android:text="9"></Button>
</RelativeLayout>
我如何给它一个固定宽度?
更新
假设我有几个按钮,我想要彼此大小相同且占整个视图(纵向)的 1/3,那么我想要一个宽度加倍的按钮。然后是一个高度加倍的按钮。我怎样才能完成手动调整它们大小的其他任务?
How can I set a fixed width for an Android button? Everytime I try to set a fixed width it fills the current parent (the RelativeView). Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativelayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<EditText android:layout_height="wrap_content" android:editable="false" android:layout_width="fill_parent" android:id="@+id/output"></EditText>
<Button android:layout_height="wrap_content" android:id="@+id/Button01" android:layout_below="@id/output" android:text="7" android:layout_width="wrap_content"></Button>
<Button android:layout_below="@id/output" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button02" android:layout_toRightOf="@+id/Button01" android:text="8"></Button>
<Button android:layout_below="@id/output" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button03" android:layout_toRightOf="@+id/Button02" android:text="9"></Button>
</RelativeLayout>
How would I give it a FIXED width?
UPDATE
Say I have several buttons that I want the same size as each other and 1/3 of the entire view (Portrait), then I want a button with double the width. Then a button with double the height. How could I accomplish this other that manually sizing them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了实现你想要的,你可以使用带有权重的 LinearLayout 。例如,您可以将 WeightSum 设置为 9。如果将里面的三个按钮的权重分别设置为 1。它们将占据 1/3 的位置,您可以为另一个对象放置 2 个位置。这个物体将是另一个物体的两倍大。
http://developer.android.com/reference/android/ widget/LinearLayout.html#setWeightSum(float)
编辑:我想补充一点,为了使其正常工作,您必须将宽度或高度设置为 0px (取决于它是水平布局还是垂直布局) 。
To accomplish what you want, you can use a LinearLayout with a weightsum. For example, you can put a WeightSum of 9. If you put the weight of three button inside to 1 each. They will take 1/3 of the place, and you can put 2 for another object. This object will be twice as big as the other.
http://developer.android.com/reference/android/widget/LinearLayout.html#setWeightSum(float)
Edit: I would like to add that for this to work correctly, you have to set the width or height to 0px (depending on if it's an horizontal or a vertical layout).
您可以使用固定像素大小,而不是
android:layout_width="wrap_content"
,例如android:layout_width="20px"
或更好的方法:android:layout_width ="20dp"
您还可以在代码中以编程方式设置宽度:
Button.setWidth(int width)
关于您的更新:
我不知道这是否是一个好的解决方案,但是我工作。您可以用来
读取屏幕分辨率,然后根据metrics.widthPixel和metrics.heightPixel设置按钮大小
Instead of
android:layout_width="wrap_content"
you can use a fixed pixelsize like for exampleandroid:layout_width="20px"
or a better way:android:layout_width="20dp"
you can also set the width programatically in the code:
Button.setWidth(int width)
Concerning your update:
I don't know if it is a good solution, but i works. You could use
to read the screen resolution and then set the button sizes according to metrics.widthPixel and metrics.heightPixel
使用 minWidth 和 minHeight 设置按钮的宽度和高度。
Use minWidth and minHeight to set the width and height of a button.
动态设置按钮的最佳和最简单的方法是
高度和宽度(以像素 px 为单位)。 45 是以 dp 为单位的高度,42 是以 dp 为单位的宽度。
因此,例如,如果您将按钮放置在 TableLayout 内的 TableRow 中,则应将其设置为 TableRow.LayoutParams
The best and easiest way to set a button dynamically is
The height and width in pixels px. 45 being the height in dp and 42 being the width in dp.
So, for example, if you've placed your button within a TableRow within a TableLayout, you should have it as TableRow.LayoutParams
在该代码中添加
android:minwidth
行。add
android:minwidth
line in that code.