簡體   English   中英

如何在 android 工作室的按鈕上方放置 label?

[英]How I can put a label above the button in android studio?

我想設計一個附加到 label 的消息按鈕,顯示消息的數量。 就像下面的圖片

在此處輸入圖像描述

我用 android studio 設計的,但是 label 沒有正確對齊。 我怎樣才能把它放在按鈕的頂部?

在此處輸入圖像描述

這是我的代碼

<?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=".HomeActivity">


    <Button
        android:id="@+id/msg"
        android:layout_width="157dp"
        android:layout_height="42dp"
        android:background="@color/lightgrey"
        android:backgroundTint="#@color/lightgrey"
        android:text="Messages"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.206" />

    <TextView
        android:id="@+id/mark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_textview"
        android:gravity="center"
        android:text="2"
        android:textColor="@color/lightgrey"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.662"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.17" />



</androidx.constraintlayout.widget.ConstraintLayout>

您可以使用BadgeDrawable

val badge = BadgeDrawable.create(this)
BadgeUtils.attachBadgeDrawable(badge, yourButton)
// usage
badge.number = 5

您需要刪除附加到父級的TextView的任何約束,並使TextView僅約束到Button

<?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=".HomeActivity">

    <Button
        android:id="@+id/msg"
        android:layout_width="157dp"
        android:layout_height="42dp"
        android:background="@color/lightgrey"
        android:backgroundTint="#@color/lightgrey"
        android:text="Messages"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="0.206"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/mark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_textview"
        android:gravity="center"
        android:text="2"
        android:textColor="@color/lightgrey"
        android:textSize="20sp"
        android:elevation="10dp"
        app:layout_constraintBottom_toTopOf="@+id/msg"
        app:layout_constraintEnd_toEndOf="@id/msg"
        app:layout_constraintStart_toEndOf="@id/msg"
        app:layout_constraintTop_toTopOf="@id/msg" />


</androidx.constraintlayout.widget.ConstraintLayout>

預習

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM