【问题标题】:Rounded view with title and text inside带有标题和文本的圆形视图
【发布时间】:2019-06-12 15:21:28
【问题描述】:

我怎样才能做到这一点?它看起来不错,我想使用这样的东西。

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

【问题讨论】:

  • 你能显示一张图片吗!
  • @SumitShukla 已经准备好了

标签: java android android-custom-view


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    实现'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) 不要忘记将您的 Base Application Theme AppTheme 更改为 Material Component 主题。

    <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>
    

    你需要使用材质组件主题!!

    【讨论】:

      猜你喜欢
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-15
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多