use*_*160 33 android button android-layout
Android默认的Button类提供了一个非常大的按钮.

周围有很多浪费的空间.现在我想创建一个非常小的按钮,它只比文本/标题略大.如何从代码中执行此操作?到目前为止,我发现了以下方法,但它仍然提供太大的按钮并浪费太多空间:
A.在res/layout中创建XML(smallbutton.xml):
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
style="?android:attr/buttonStyleSmall"
android:text="color"
android:padding="0dp"
android:layout_margin="0dp"
android:textSize="8dp"
android:maxHeight="2dp"
android:maxWidth="2dp"
/>
Run Code Online (Sandbox Code Playgroud)
B.从您的代码中膨胀并添加到视图中:
Button pickBackColor = (Button) this.getLayoutInflater().inflate(R.layout.smalbutton,null);
Run Code Online (Sandbox Code Playgroud)
...
LinearLayout linLayout = new LinearLayout(this);
linLayout.addView(pickBackColor);
Run Code Online (Sandbox Code Playgroud)
现在问题是按钮仍然太大,它占用了文本周围(在左侧,右侧,上方,下方)的太多空间.

任何提示如何进一步减小按钮的大小?
Maa*_*ngs 126
按钮具有最小的高度和宽度(默认情况下通常为48dp,分别为64dp).所以加
android:minHeight="0dp"
android:minWidth="0dp"
Run Code Online (Sandbox Code Playgroud)
得到非常小的按钮:

为了您的信息,Android还提供了默认样式,用于定义较小的按钮:
style="?android:attr/buttonStyleSmall"
Run Code Online (Sandbox Code Playgroud)
您还可以根据需要修改此默认样式,并在文件styles.xml中定义自定义属性.以下是问题中描述的修改示例:
<style name="MyButtonStyleSmall" parent="android:Widget.Button.Small">
<!-- Customizations -->
<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后你只需要在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" />
Run Code Online (Sandbox Code Playgroud)
在.xml文件中...您需要将小值传递给xml文件中的layout_height和layout_width...尝试这个。
android:layout_width="50dp" android:layout_height="50dp"
Run Code Online (Sandbox Code Playgroud)
根据您的要求更改值...
| 归档时间: |
|
| 查看次数: |
27300 次 |
| 最近记录: |