繁体   English   中英

圆角按钮不起作用

[英]rounded corner for button not working

我从服务器获取颜色代码 #3a87ad设置为背景,并且我还尝试为按钮设置圆角形状。 但是,它始终将黑色显示为背景。

tv_img_tag = (Button) vi.findViewById(R.id.tv_img_tag);

tv_img_tag.setBackgroundColor(Color.parseColor(product
                .get("stop_status_color")));   

 tv_img_tag.setBackgroundResource(R.drawable.roundedtexts);

     tv_img_tag.setText(product.get("stop_status_name").toString());

roundedtexts.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>


    <corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />
    <padding

        android:left="8dp"
        android:top="8dp"
        android:right="8dp"
        android:bottom="8dp" />
</shape>

嗨,兄弟,请尝试使用此工具并创建精彩的android button.Tool也为您提供源代码。 愤怒的工具

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="8dp" />

    <solid android:color="#54483C" />

    <padding
        android:bottom="8dp"
        android:left="8dp"
        android:right="8dp"
        android:top="8dp" />

</shape>

首先,您的roundedtexts.xml在drawable中没有定义的颜色。 您必须通过设置< Solid >标签的颜色来为xml设置一些颜色。

在您的课程文件中排第二。 您要先设置背景色,然后再将drawable设置为背景。 因此,这里发生的是您最后一次设置背景的呼叫未设置为颜色。

为此,您必须将类中的xml drawable实例转换为gradientDrawable ,然后set your dynamic color to that gradientDrawable instance ,现在您可以将该实例设置为以背景显示。

问题是先设置颜色,然后设置背景可绘制,但是在roundedtexts.xml中您没有指定任何颜色,因此它给了您黑色背景,只是尝试在roundedtexts.xml文件中添加背景色,如透明色。

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

<solid android:color="@android:color/transparent"/>

<corners 
    android:topLeftRadius="0dp"
    android:topRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="0dp"/>

<padding 
    android:left="2dp" 
    android:top="2dp" 
    android:right="2dp"                  
    android:bottom="2dp" />

使用以下代码

tv_img_tag = (Button) vi.findViewById(R.id.tv_img_tag);
tv_img_tag.setBackgroundResource(R.drawable.roundedtexts);
GradientDrawable sd = (GradientDrawable)  tv_img_tag.getBackground().mutate();
sd.setColor(0xff999999);
sd.invalidateSelf();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM