【问题标题】:Android button background is covering up button textAndroid按钮背景掩盖了按钮文字
【发布时间】:2014-11-15 04:41:23
【问题描述】:

我有一个看起来像我想要的按钮。唯一的问题是它掩盖了文本。我知道它覆盖了它并且文本显示在它后面,因为如果我删除 android:background 属性,则会显示文本。

我的目标是拥有按钮,其文本位于按钮顶部。

这是我的按钮:

<Button
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:layout_margin="30dp"
    android:background="@drawable/timecrunchbutton"
    android:text="Test"
    android:textColor="@color/white"/>

还有timecrunchbutton.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- Top 1dp Shadow -->
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/blue" />
                    <corners android:radius="7dp" />
                </shape>
            </item>
            <!-- Light bottom color -->
            <item android:top="1dp">
                <shape android:shape="rectangle">
                    <solid android:color="@color/darkblue" />
                    <corners android:radius="7dp" />
                    <stroke android:width="1px" android:color="#248349"/>
                    <padding
                        android:top="28dp"
                        android:bottom="36dp" />
                </shape>
            </item>
        </layer-list>
    </item>
    <item>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- Bottom 3dp Shadow -->
            <item>
                <shape android:shape="rectangle">
                    <solid android:color="@color/darkblue" />
                    <corners android:radius="7dp" />
                </shape>
            </item>
            <!-- Light top color -->
            <item android:bottom="3dp">
                <shape android:shape="rectangle">
                    <solid android:color="@color/blue" />
                    <corners android:radius="7dp" />
                    <stroke android:width="1px" android:color="#22A863"/>
                    <padding
                        android:top="36dp"
                        android:bottom="28dp" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

【问题讨论】:

    标签: android xml button layout background


    【解决方案1】:

    好吧,这听起来可能很愚蠢,但我觉得我也必须问这个问题:你的@drawable/timecrunchbutton 是白色的吗?还是里面有很多白色?

    【讨论】:

    • 我已经包含了它的xml,所以你可以看到,但它都是蓝色和深蓝色,所以它不应该影响显示的文本。
    • 同意。我会考虑并提出可能的解决方案。
    • 我每天都检查这个帖子@Vlad,等待你可能的解决方案......
    • 我一直没有在这附近活动,根本没有时间,但是你的回复让我笑了一点! Island Wave 给了你正确的答案,所以我在这里没有任何帮助,对不起。
    【解决方案2】:

    尝试使用

    android:layout_height="wrap_content"

    在您的 Button 定义中,而不是

    android:layout_height="65dp"

    【讨论】:

    • 太棒了!你知道为什么吗?明确定义按钮的高度会很好。
    • 我以前也遇到过这种情况,所以我在您的代码中注意到了它。我认为这与使用固定大小和自定义可绘制对象时扩展布局的方式有关。我通常尽量不将高度设置为固定,但宽度似乎可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多