如何以编程方式设置主屏幕小部件上使用的形状的描边颜色

问题描述 投票:0回答:1

所以,我有这个 rounded_corner_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="1dp"
        android:color="TO_BE_SET_DYNAMICALLY" />

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

用于 ip_properties.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/ip_property_tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corner_shape" />
</LinearLayout>

然后我想从 RemoteViewsService.RemoteViewsFactory 实现动态设置描边颜色,如下所示:

class WifiPropertyViewsFactory: RemoteViewsService.RemoteViewsFactory{
    // other overrides...

    override fun getViewAt(position: Int): RemoteViews =
       RemoteViews(context.packageName, R.layout.ip_properties)
                .apply {
                    setTextViewText(R.id.ip_property_tv_1, "some data")
                    // set background rounded corner shape stroke color
                }

}

由于不太了解,我尝试通过

RemoteViews.setInt(R.id.ip_property_tv_1, "setBackgroundTint", colorInt)
设置,但没有成功。有人知道如何让它发挥作用吗?

android kotlin android-layout android-widget remoteview
1个回答
0
投票

尝试此代码 - 文本视图以编程方式更改背景描边颜色 在 setOnClickListener() 方法中使用此代码

edtSelectState.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setStroke(2, Color.RED);

            edtSelectState.setBackground(gradientDrawable);
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.