android - 将形状设置为 ImageView 背景以获得圆角
问题描述
我正在尝试使用ConstraintLayout在ScrollView内构建带有圆角的布局。在布局的顶部,我想放置一个顶角圆角(以匹配布局角)和底角笔直的图像。
按照我在这个StackOverflow 问题中找到的建议,这是我的代码:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ScrollView
android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_weight="7.5"
android:background="@color/darkModePrimary">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:background="@drawable/layout_bg"
android:layout_height="wrap_content">
<ImageView
android:background="@drawable/layout_bg"
android:src="@drawable/house_medium"
android:id="@+id/house_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_marginTop="32dp"
android:gravity="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/house_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some text"
android:textSize="24sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我为 ConstraintLayout 的背景定义形状的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white"/>
<corners android:radius="10dp"/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
如果我将此形状设置为约束布局和 ImageView 的背景,我会得到 - 正如预期的那样 - 具有所有四个圆角的图像,如此处所示。
我尝试创建另一个形状用作图像的背景,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white"/>
<corners android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"/>
</shape>
但是当我将它设置为ImageView的背景时,我没有得到我想要的,图像的所有四个角现在都是直的
我究竟做错了什么?
解决方案
要使其正常工作,只需在代码中为您的卡片容器布局设置大纲。
在布局 xml 中将 id 添加到卡片容器:
<androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/card" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:layout_marginTop="16dp" android:layout_marginRight="16dp" android:background="@drawable/layout_bg" android:elevation="4dp">
在您的 MainActivity onCreate 中:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) card.clipToOutline = true card.outlineProvider = ViewOutlineProvider.BACKGROUND }
推荐阅读
- python - cross_val_score 和 StratifiedKFold 之间的 F-Score 差异
- python - 如何将此日期格式化为日-月-年
- javascript - 使用正则表达式在数组中拆分字符串,保留数组的所有单词
- html - headbreaking problems with bottom margins css grid
- sas - 发生 DBMS 错误时,是否可以使用 SAS Enterprise 中的代码导入多个 Excel 工作表?
- docker - Kubernetes - 如何设置共享相同存储库/管道的并行集群(Prod、Dev)
- javascript - Javascript 节点获取用法
- laravel - 如何在雄辩的 laravel 命令中转换此 sql 查询?
- php - WP 插件:为什么我需要一个库的依赖项?
- python - 检验回归系数等于 statsmodels OLS 中的非零值的零假设