如何在不侵犯边界的情况下以编程方式更改背景颜色?

时间:2018-05-03 06:18:13

标签: android android-layout android-drawable android-color

我想创建一个带圆角的正方形,并在形状内添加背景颜色,并为其添加边框颜色,如下图所示:

Expected design

但我只能做到这一点:

Result

我有一个需要为每个圆角方框及其动态数据添加的颜色列表,因此任何数量的颜色都将从API捕获并显示在recyclerview上。但是当我更改布局的背景颜色或背景颜色时,颜色会出现,背景边框会消失。

这是我的xml代码:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp">
        <FrameLayout
            android:id="@+id/colorframe"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:background="@drawable/status_color_white_square"
            android:layout_centerInParent="true"
            android:onClick="@{()->viewModel.onItemClick()}">


            <com.monstarlab.instantmac.ui.custom.IMTextView
                android:id="@+id/captionET"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="A"
                android:visibility="@{safeUnbox(viewModel.isSelected ? View.VISIBLE:View.GONE )}" />

        </FrameLayout>
</RelativeLayout>

带边框的drawable:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomRightRadius="6dp"
    android:bottomLeftRadius="6dp"
    android:topLeftRadius="6dp"
    android:topRightRadius="6dp"/>

<stroke android:width="1dp"
    android:color="#e8e9ea"/>

适配器视图中的Java代码onBind()方法:

        final StatusColor color = colorlist.get(position);

        statusColorViewModel = new StatusColorViewModel(color, this);

        mBinding.setViewModel(statusColorViewModel);

        mBinding.colorframe.setBackgroundColor(ContextCompat.getColor(mContext, color.getColor()));

如何在不影响边框的情况下实现颜色变化?

3 个答案:

答案 0 :(得分:0)

尝试以下代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="1dp"
        android:color="#000000" />//corner color
    <corners
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"

        />

    <solid android:color="#8058595b" />// inside Color 

</shape>

动态更改以上纯色:

GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);

对于下面的笔划使用:

GradientDrawable myGrad = (GradientDrawable)rectangle.getBackground();
myGrad.setStroke(2, Color.RED);

答案 1 :(得分:0)

你好这个代码

1:xml的代码:

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

    <RelativeLayout
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_centerInParent="true"
        android:background="@drawable/status_color_white_square">

        <FrameLayout
            android:id="@+id/colorframe"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:layout_centerInParent="true"
            android:layout_margin="1dp"
            android:background="@drawable/bg">

            <TextView
                android:id="@+id/captionET"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="A" />

        </FrameLayout>
    </RelativeLayout>

</RelativeLayout>

2:可绘制的:   I)bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:bottomLeftRadius="6dp"
        android:bottomRightRadius="6dp"
        android:topLeftRadius="6dp"
        android:topRightRadius="6dp" />

    <solid android:color="#cf0000"/>

</shape>

II)bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:bottomLeftRadius="6dp"
        android:bottomRightRadius="6dp"
        android:topLeftRadius="6dp"
        android:topRightRadius="6dp" />

    <stroke
        android:width="1dp"
        android:color="#e8e9ea" />
</shape>

3:Java代码

import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;

public class Temp extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temp);


        FrameLayout colorframe = (FrameLayout) findViewById(R.id.colorframe);



        ViewCompat.setBackgroundTintList(
                colorframe,
                ColorStateList.valueOf(/*dynamic color enter here*/getResources().getColor(R.color.green)));
    }
}

答案 2 :(得分:0)

这也可以通过将文本放在中心来创建相对布局和文本视图来实现。使用drawable资源文件为布局应用background属性。

main_activity.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#3F51B5">
    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/border">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Google"
            android:id="@+id/txt_google"
            android:layout_centerInParent="true"
            android:textStyle="bold"
            android:textColor="@color/colorAccent"/>
    </RelativeLayout>
    </LinearLayout>

Create drawable resource border.xml and put the below shape and stoke properties



   <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:bottomRightRadius="6dp"
            android:bottomLeftRadius="6dp"
            android:topLeftRadius="6dp"
            android:topRightRadius="6dp"/>
        <solid
            android:color="@android:color/darker_gray">
        </solid>
        <stroke android:width="1dp"
            android:color="@color/colorAccent"/></shape>