首页 > 解决方案 > 在 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>

标签: androidandroid-layout

解决方案


您可以参考以下代码:

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>

推荐阅读