【发布时间】:2018-05-03 06:18:12
【问题描述】:
我想创建一个带圆角的正方形,并在形状内添加背景颜色,并为其设置边框颜色,如下图所示:
但我只能做到这一点:
我有一个需要为每个圆角方框及其动态数据添加的颜色列表,以便从 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()));
如何在不影响边框的情况下实现颜色变化?
【问题讨论】:
-
您可以创建另一个
drawable文件吗? -
@CagriYalcin 你能详细说明一下吗?
标签: android android-layout android-drawable android-color