android - 在 Android 上的 Divider 上添加圆形 TextView 或 Button
问题描述
我是安卓新手。在我的一个视图中,我需要在分隔视图顶部添加一个圆形 TextView 或 Button 吗?按钮需要在分隔线的顶部水平和垂直居中。我该怎么做呢?我搜索了很多来源,但没有找到确切的东西。任何人都可以帮我这样做吗?
我按照以下方式创建分隔线:
<ImageView
android:id="@+id/aaa"
android:layout_width="match_parent"
android:layout_height="@dimen/_2sdp"
android:layout_gravity="center"
android:layout_marginStart="@dimen/_20sdp"
android:layout_marginTop="@dimen/_30sdp"
android:layout_marginEnd="@dimen/_20sdp"
android:src="@drawable/gradient_divider" />
风格如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#e5eaff"
android:startColor="#f3f5f9" />
</shape>
解决方案
您可以参考以下代码:
shape_circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#ffff" />
<stroke android:width="1dp" android:color="#2222"/>
</shape>
shape_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="1dp"
android:color="#000000" />
<size android:height="2dp" />
</shape>
并像这样使用它ConstraintLayout
:
<?xml version="1.0" encoding="utf-8"?>
<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">
<ImageView
android:layout_width="match_parent"
android:layout_height="10dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="20dp"
android:background="@drawable/with_layer"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle"
android:gravity="center"
android:padding="4dp"
android:text="OR"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="10dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
推荐阅读
- extjs - 将记录添加到存储并显示在网格上
- php - PHP如何增加session.gc_maxlifetime?更改 php.ini 不起作用
- linux - 可以在跨操作系统网络共享上使用 git 吗?
- c++ - 试图让 i2c 压力传感器将数据发送到 ATtiny85,但我可用的库给了我“未声明”的错误
- vue.js - Vuetify v-menu 组件不可见
- azure - 通过自定义策略将多个社交帐户链接到 Azur B2C 本地帐户
- c# - 如何使用 GoogleApiAvailability?我想使用它时总是收到错误消息
- python - 在 Anaconda 提示符中更新软件包时的 RemoveError
- r - 使用 R 和带有完整链接方法的凝聚层次聚类 (AHC) 技术绘制树状图
- node.js - 无法将 tesseract 函数的结果值存储到异步函数内的全局变量中