以编程方式更改Drawable的背景,保持圆角

4

interview_timeline_row.xml

<LinearLayout
    android:id="@+id/interviewTimelineIconLayout"
    android:layout_width="52dp"
    android:layout_height="52dp"
    android:layout_marginTop="20dp"
    android:background="@drawable/timeline_row_icon_layout_bg"
    android:gravity="center"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/interviewTimelineRowIcon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:adjustViewBounds="false"
        android:cropToPadding="false"
        android:padding="6dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>

timeline_row_icon_layout_bg.xml

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

    <corners android:radius="@dimen/_50sdp" />

    <stroke android:width="1dp" android:color="@color/white" />

    <solid android:color="@color/ic_rescheduled"/> //need to add this programatically

</shape>

InterviewTimeline.java

iconBg = row.findViewById(R.id.interviewTimelineIconLayout);

iconBg.setBackgroundColor(getResources().getColor(R.color.ic_rescheduled)); //this is the wrong way to go about it

我想在应用程序的各个位置使用timeline_row_icon_layout_bg.xml文件,并且每次应该有不同的背景颜色。如果我使用iconBg.setBackgroundColor()方法,则会忽略半径,我会得到一个正方形的背景颜色。


1
你无法通过编程将某些内容添加到资源文件中。但你可以将你的 ImageView 包裹在一个 CardView 中,并给它一个圆角和颜色。 - OhhhThatVarun
3个回答

4

如果您只是使用形状创建具有圆角和边框的布局,则第一种选择是将您的LinearLayout包装在CardView中,然后对该卡应用圆角半径、描边和背景色。

否则,您可以使用Material Components库中包含的MaterialShapeDrawable绘制自定义形状

只需从LinearLayout中删除android:background

<LinearLayout
    android:id="@+id/interviewTimelineIconLayout"
    android:layout_width=".."
    android:layout_height="..
    ..>

    <!-- ..... -->

</LinearLayout>

然后在您的代码中,您可以应用一个ShapeAppearanceModel。类似于:

        float radius = getResources().getDimension(R.dimen.corner_radius);

        LinearLayout linearLayout= findViewById(R.id.interviewTimelineIconLayout);
        ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
            .toBuilder()
            .setAllCorners(CornerFamily.ROUNDED,radius)
            .build();

        MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
        //Fill the LinearLayout with your color
        shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.yourColor));
        //Stroke color and width
        shapeDrawable.setStrokeWidth(2.0f);
        shapeDrawable.setStrokeColor(...);

        ViewCompat.setBackground(linearLayout,shapeDrawable);

enter image description here

以这种方式,您可以轻松更改和设置背景颜色和描边。

1

复制您的“timeline_row_icon_layout_bg.xml”文件并将其重命名为“timeline_row_icon_layout_bg_new.xml”。


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

    <corners android:radius="@dimen/_50sdp" />

    <stroke android:width="1dp" android:color="@color/white" />

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

</shape>

而且您可以将背景设置为布局:

iconBg.setBackgroundResource(R.drawable.timeline_row_icon_layout_bg_new);

感谢回复,但应该有比这更简单的方法,否则我将需要创建多个文件以给每个图像设置不同的背景颜色。 - Abd Maj
首先,您设置背景,使用带有圆角的形状资源。 然后,您将背景替换为新的层(颜色)。这将丢失圆角。您还有另一种选择,使用CardView。将您的布局放入CardView中,然后简单地添加:app:cardBackgroundColor="your_color" app:cardCornerRadius="20dp" - Dilshod

0

以编程方式设置LinearLayout的背景。

val bgDrawable = resources.getDrawable(R.drawable.timeline_row_icon_layout_bg, null).apply{
    colorFilter = PorterDuffColorFilter(
            ResourcesCompat.getColor(resources, R.color.ic_rescheduled, null),
            PorterDuff.Mode.SRC_IN
        )

}

iconBg.background = bgDrawable

将形状可绘制文件timeline_row_icon_layout_bg移动到可绘制资源文件夹中。

R.drawable.no_data_image是什么?timeline_row_icon_layout_bg.xml是一个可绘制的资源文件,但是你上面调用了一个颜色资源文件。 - Abd Maj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接