如何从代码在android中创建非常小的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10258823/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:02:29  来源:igfitidea点击:

How to create really small buttons in android from code?

androidandroid-layoutbutton

提问by user898160

Android default Button class provides a really big button.

Android 默认 Button 类提供了一个非常大的按钮。

button with caption "Save"

带有“保存”标题的按钮

There is a lot of wasted space around. Now I want to create really small buttons, that are only slightly bigger than the text/caption. How to do this from code? So far I found the following method, but it is still providing too big buttons and wasting too much space:

周围有很多浪费的空间。现在我想创建非常小的按钮,只比文本/标题稍大。如何从代码中做到这一点?到目前为止,我找到了以下方法,但它仍然提供了太大的按钮并浪费了太多空间:

A. Create an XML (smallbutton.xml) in the res/layout:

A. 在 res/layout 中创建一个 XML (smallbutton.xml):

<?xml version="1.0" encoding="utf-8"?>

    style="?android:attr/buttonStyleSmall"
    android:text="color"
    android:padding="0dp"
    android:layout_margin="0dp"

    android:textSize="8dp"
    android:maxHeight="2dp"
    android:maxWidth="2dp"

/>

B. From your code inflate this and add to the view:

B. 从你的代码中膨胀这个并添加到视图中:

Button pickBackColor = (Button) this.getLayoutInflater().inflate(R.layout.smalbutton,null);

...

...

LinearLayout linLayout = new LinearLayout(this);

linLayout.addView(pickBackColor);

Now the problem is that the button is still too big, it uses too much space around (on the left, right, above, under) of the text.

现在的问题是按钮仍然太大,它在文本周围(左侧、右侧、上方、下方)使用了太多空间。

enter image description here

在此处输入图片说明

Any hint how to decrease the size of the button further?

任何提示如何进一步减小按钮的大小?

回答by Maarten Pennings

Buttons have a minimal height and width (typically of 48dp respectively 64dp by default). So add

按钮具有最小的高度和宽度(默认情况下通常分别为 48dp 和 64dp)。所以添加

android:minHeight="0dp"
android:minWidth="0dp"

to get really small buttons:

获得非常小的按钮:

enter image description here

在此处输入图片说明

回答by Yoann Hercouet

For your information there is also the default style provided by Android that defines smaller buttons:

供您参考,Android 还提供了定义较小按钮的默认样式:

style="?android:attr/buttonStyleSmall"

You can also if necessary modify this default style and define custom attributes in your file styles.xml. Here is an example with the modifications described in the question:

如有必要,您还可以修改此默认样式并在您的文件 styles.xml 中定义自定义属性。以下是问题中描述的修改示例:

<style name="MyButtonStyleSmall" parent="android:Widget.Button.Small">
    <!-- Customizations  -->
    <item name="android:minHeight">0dp</item>
    <item name="android:minWidth">0dp</item>
</style>

Then you just need to call it in the XML:

然后你只需要在 XML 中调用它:

<Button 
   android:id="@+id/small_button"
   android:text="@string/example"
   android:contentDescription="@string/example_desc"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   style="@style/MyButtonStyleSmall" />

回答by Sandip Armal Patil

in .xml file... you need to pass small value to layout_height and layout_width in xml file...... try this.

在 .xml 文件中...您需要将小值传递给 xml 文件中的 layout_height 和 layout_width ......试试这个。

android:layout_width="50dp" android:layout_height="50dp"  

Change value as per your requirement...

根据您的要求更改值...

回答by Boe-Dev

try this in your code:

在你的代码中试试这个:

pickBackColor.setWidth(VALUE);
pickBackColor.setHeight(VALUE);