How to create a widget button with rounded corners and set any colour

问题: I am trying to create a widget button with rounded corners which I can also change to ANY colour I want. I currently use an xml layout file to create the rounded corners. T...

问题:

I am trying to create a widget button with rounded corners which I can also change to ANY colour I want. I currently use an xml layout file to create the rounded corners. The problem is when I change the colour the button returns to its rectangular shape. I would like for it to maintain at least its rounded corners.

I can't find a solution for this but any ideas gratefully received. It may not be possible but thought I'd ask. The main point is that I am trying to do this with remote views as part of a widget NOT in an activity which is easy to do.

Happy to consider any options...

Thank you

RemoteViews views;
views = new RemoteViews(context.getPackageName(), R.layout.mycustomlayout);
views.setInt(R.id.widgetbutton,"setBackgroundColor", Color.parseColor(widgethexcolor));

回答1:

Create a layout resource file in drawable folder and call that as android:backgroud="@drawable/shape.xml" inside properties of button.

Instead of Gradient you can use Solid also

Code for shape.xml

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

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

<gradient
    android:angle="45"
    android:centerX="35%"
    android:centerColor="#00d6a3"
    android:startColor="#9e9c97"
    android:endColor="#ffffff"
    android:type="linear"
    />
<size
    android:height="45dp"
    android:width="250dp"
    />
</shape>

回答2:

First create a resource file inside drawable folder, named for example : bg_white_rounded.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="@dimen/space5"/>
    <solid android:color="@android:color/white"/>
</shape>

then at your button layout add : android:background="@drawable/bg_white_rounded.xml"

Ps: change the wanted dimension of the space radius.

  • 发表于 2019-01-13 14:32
  • 阅读 ( 165 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除