Android - 按钮背景色会使按钮高度缩小

3
我有一个按钮。
<Button
android:id="@+id/learn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop ="5dp"
android:text="Hello StackOverflow"
style="@style/home_page_buttons"    
/>   

我正在尝试为它添加这种样式

<style name="home_page_buttons">  
    <item name="android:layout_marginLeft">15dp</item>
    <item name="android:layout_marginRight">15dp</item>

    <item name="android:textColor">@color/light_best_blue</item> 
    <item name="android:textStyle">bold</item>   
    <item name="android:color">@color/white</item>  
     <item name="android:background">@color/white</item>

</style>

这行代码:
 <item name="android:background">@color/white</item>

将背景变为我想要的白色,但出于某种原因,它缩小了按钮的高度,并使其变得难看。

有什么想法是为什么会发生这种情况以及如何防止此副作用?

谢谢!

4个回答

4
ABDOU说得对,这可能是你的解决方案。
<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle">
    <gradient android:startColor="#FFFFFF" android:endColor="#BBBBBB" 
            android:angle="270"/>
    <stroke android:width="2dp"
            android:color="#555555" />
    <corners android:bottomRightRadius="7dp"
        android:bottomLeftRadius="5dp" 
        android:topLeftRadius="5dp"
        android:topRightRadius="7dp"/>    
</shape>

将这个内容作为XML文件保存在drawable文件夹中,并将其设置为你的BackgoundColor

你的意思是把这个 XML 文件放到通常存放图片的目录里吗? - Genadinik
是的,我把我的文件放到drawable文件夹中,然后使用"backgoundColor="@drawable/yourfile""来设置按钮的背景。 - Boe-Dev

3
默认按钮有一个固定的最小高度,等于底层图像的高度。在这里,由于您使用的是颜色,它没有最小高度。因此,它占用文本的高度。对于您的情况,您将需要固定按钮的高度。
更好的方法是使用自定义按钮图像。

我明白了,但我没有快速生成图像的方法。有没有其他不涉及图像的方式? - Genadinik
你可以使用Shape可绘制对象。http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape - Kumar Bibek

1

当你使用style="@style/home_page_buttons"时,你可以改变安卓按钮的默认样式。

如果你想保留它,只需为你的按钮制作一个背景图片即可。


0

我不知道这是版本相关的还是由于最近的更改。因为我有一个非常新的Android Studio版本,但我似乎通过指定填充属性来成功地恢复了额外的空间。

只需添加此属性:

android:padding="5dp"  

按钮将恢复失去的大部分空间。我建议调整数量,或者可能将属性拆分为其4个方向变体,以根据您想要的间距自定义间距。
这似乎也适用于 EditText 和 Button,因为两者都似乎存在相同的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接