如何从code机器人创造真正的小按钮?机器人、按钮、code

2023-09-05 06:05:10 作者:稳握江山权

Android的默认Button类提供了一个真正的大按钮。

Android default Button class provides a really big button.

有很多浪费的空间周围。现在,我要创建真正的小按钮,这仅仅比文本/标题稍微大一点。如何从code做到这一点? 到目前为止,我发现下面的方法,但它仍然提供过大的按钮和浪费太多的空间:

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:

一个。创建在res /布局XML(smallbutton.xml):

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

<?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"

/>

乙。从你的code夸大这一点,并添加到视图:

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

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.

任何提示如何减少按钮的尺寸进一步?

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

推荐答案

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

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

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

要得到真正的小按钮:

 
精彩推荐
图片推荐