Java 在Android中设置按钮的宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2796219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set width of Button in Android
提问by Mohit Deshpande
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:
如何为 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>
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?
我如何给它一个固定的宽度?
更新
假设我有几个按钮,我想要彼此相同的大小和整个视图(纵向)的 1/3,然后我想要一个宽度加倍的按钮。然后是一个高度加倍的按钮。我怎么能完成另一个手动调整它们的大小?
采纳答案by Jean-Philippe Jodoin
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.
要完成您想要的操作,您可以使用带有权重和的 LinearLayout。例如,您可以将 WeightSum 设为 9。如果将里面的三个按钮的权重设为 1。它们将占据 1/3 的位置,您可以为另一个对象放置 2。这个对象将是另一个对象的两倍大。
http://developer.android.com/reference/android/widget/LinearLayout.html#setWeightSum(float)
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).
编辑:我想补充一点,为了使其正常工作,您必须将宽度或高度设置为 0px(取决于它是水平布局还是垂直布局)。
回答by RoflcoptrException
Instead of android:layout_width="wrap_content"
you can use a fixed pixelsize like for example android:layout_width="20px"
or a better way: android:layout_width="20dp"
而不是android:layout_width="wrap_content"
您可以使用固定像素大小,例如android:layout_width="20px"
或更好的方法:android:layout_width="20dp"
you can also set the width programatically in the code: Button.setWidth(int width)
您还可以在代码中以编程方式设置宽度: Button.setWidth(int width)
Concerning your update:
关于您的更新:
I don't know if it is a good solution, but i works. You could use
我不知道这是否是一个好的解决方案,但我可以工作。你可以用
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
to read the screen resolution and then set the button sizes according to metrics.widthPixel and metrics.heightPixel
读取屏幕分辨率,然后根据 metrics.widthPixel 和 metrics.heightPixel 设置按钮大小
回答by Muhammad Aamir Ali
Use minWidth and minHeight to set the width and height of a button.
使用 minWidth 和 minHeight 设置按钮的宽度和高度。
<Button android:layout_height="wrap_content"
android:id="@+id/Button01"
android:layout_below="@id/output"
android:text="7"
android:minWidth="100dp">
</Button>
回答by Ali Kahoot
The best and easiest way to set a button dynamically is
动态设置按钮的最佳和最简单的方法是
Button index=new Button(this);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics());
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 42, getResources().getDisplayMetrics());
The height and width in pixels px. 45 being the height in dpand 42 being the width in dp.
以像素为单位的高度和宽度px。45是在高度DP和42是在宽度DP。
index.setLayoutParams(new <Parent>.LayoutParams(width, height));
So, for example, if you've placed your button within a TableRow within a TableLayout, you should have it as TableRow.LayoutParams
因此,例如,如果您已将按钮放置在 TableLayout 内的 TableRow 中,则应将其作为 TableRow.LayoutParams
index.setLayoutParams(new TableRow.LayoutParams(width, height));
回答by Rathiga Jesika
add android:minwidth
line in that code.
android:minwidth
在该代码中添加行。