带有标题和文本的圆形视图

Rounded view with title and text inside

我该怎么做?看起来不错,我想用这样的东西。

顶部的行应对应标题的末尾

实际上,这些类型的视图大多是用自定义视图完成的。

This tutorial is useful 用于自定义视图。

不过你可以稍微作弊,在drawable文件夹中创建backgound.xml,然后粘贴代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">

    <stroke android:color="@color/blue" android:width="4dp"/>
    <corners android:radius="10dp"/>
    <solid android:color="@color/white"/>
</shape>

然后为您的布局创建 custom_background.xml,并粘贴以下代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <LinearLayout

            android:background="@drawable/background"

            android:layout_marginTop="15dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView
                android:textSize="20sp"
                android:textAlignment="center"
                android:textColor="@color/blue"
                android:layout_gravity="center"
                android:text="Something"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

    </LinearLayout>
    <TextView
            android:padding="2dp"
            android:textColor="@color/blue"
            android:textSize="25sp"
            android:background="@color/white"
            android:layout_marginStart="50dp"
            android:text="Title"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

然后你会得到如下图

备注

  • 我正在使用 androidX
  • 您可以放置​​任何布局而不是 LinearLayout

1) 将您的项目迁移到 androidx.
2) 在您的 build.gradle 中包含依赖项:

implementation 'com.google.android.material:material:1.0.0'

3) 在您的布局中使用:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text">

  <com.google.android.material.textfield.TextInputEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

4) 不要忘记将您的基本应用程序主题 AppTheme 更改为 Material 组件主题。

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>

您需要使用material组件主题!!