Tout nouveau dans le développement Android ... J'ai un texte de badge sur un bouton, mais pour une raison quelconque, le texte n'est pas aligné sur le centre.Badge TextView Le texte n'est pas aligné au centre
J'ai créé un nouveau fragment et mis Component Tree
-relativeLayout
J'ai ajouté le bouton et le textview.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="200dp">
<Button
android:id="@+id/intercom_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="40dp"
android:background="@drawable/ic_intercom_logo"
android:elevation="10dp"
android:outlineProvider="bounds"
android:shadowColor="@color/brownishGrey"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/intercom_badge_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/intercom_button"
android:layout_alignRight="@id/intercom_button"
android:layout_alignTop="@id/intercom_button"
android:layout_marginEnd="8dp"
android:layout_marginTop="35dp"
android:background="@drawable/badge_circle"
android:elevation="11dp"
android:paddingEnd="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="10"
android:textAlignment="center"
android:gravity="center"
android:textColor="#FFF"
android:textSize="10dp"
app:layout_constraintEnd_toEndOf="@+id/intercom_button"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
et drawable/badge_circle
je l'ai utilisé.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false"
android:thickness="8dp"
android:innerRadius="0dp"
>
<solid
android:color="#F00"
/>
<stroke
android:width="1dip"
android:color="#F00" />
<padding
android:top="2dp"
android:bottom="2dp"/>
</shape>
Il en résulte
D'après ce que je comprends cela se produit parce que les propriétés de TextView mis à
layout_width:wrap_content
layout_height:wrap_content
Mais si je les ai mis à des contraintes statiques comme
layout_width:16dp
layout_height:16dp
Le texte ne s'aligne toujours pas. Aussi la mise en gravité ou TextAlignment ne contribue pas
android:textAlignment="center"
android:gravity="center"
Comment puis-je résoudre ce problème?
avez-vous ajouté un tag central sur notre android.support.constraint.ConstraintLayout? – DroiDev
Ai-je bien compris? le problème est que le "10" n'est pas dessiné au milieu du cercle rouge? – 0X0nosugar
Y at-il une raison pour ne pas simplement ajouter le texte au bouton avec 'android: text =" 10 "'. Il devrait automatiquement centrer pour vous. – windedmoose