简体   繁体   English

圆角按钮不起作用

[英]rounded corner for button not working

I am getting color code #3a87ad from the server to set as the background, and I am also trying to give a rounded corner shape to my button. 我从服务器获取颜色代码 #3a87ad设置为背景,并且我还尝试为按钮设置圆角形状。 However it always shows the black color as background. 但是,它始终将黑色显示为背景。

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

Hi Brother try this tool and create wonderful android buttons.Tool provides you source code as well. 嗨,兄弟,请尝试使用此工具并创建精彩的android button.Tool也为您提供源代码。 Angry Tools 愤怒的工具

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

First your roundedtexts.xml has no defined color in drawable. 首先,您的roundedtexts.xml在drawable中没有定义的颜色。 You have to set some color to xml by setting color of < Solid > tag. 您必须通过设置< Solid >标签的颜色来为xml设置一些颜色。

Second in your class file. 在您的课程文件中排第二。 You are setting background color first and then setting drawable to background. 您要先设置背景色,然后再将drawable设置为背景。 So here happening is your last call to set background is set not color. 因此,这里发生的是您最后一次设置背景的呼叫未设置为颜色。

To do so, you have to get that xml drawable instance in your class into gradientDrawable and then have to set your dynamic color to that gradientDrawable instance and Now you can set that instance to view as background. 为此,您必须将类中的xml drawable实例转换为gradientDrawable ,然后set your dynamic color to that gradientDrawable instance ,现在您可以将该实例设置为以背景显示。

Problem is you set color first and then you set background drawable but in roundedtexts.xml you didn't specify any color, so it gives you black background just try to add background color in roundedtexts.xml file like transperent color. 问题是先设置颜色,然后设置背景可绘制,但是在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" />

Use below code 使用以下代码

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