简体   繁体   中英

Android: text not being centered inside button/textview with fixed width/height

I'm having an issue trying to do something really simple: Center text in a button/TextView that have a fixed width and height, my text keeps being pushed down as I increase its size, here's a sample of the code I'm using:

<Button
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:background="@drawable/circle_add_item"
            android:gravity="center"
            android:text="+"
            android:textColor="@color/green"
            android:textSize="75sp" />

Here's an image of what I'm getting as a result:

在此处输入图片说明

Would anyone give me a hand ?

Thanks !

Also, you can use this way

<RelativeLayout
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:background="@drawable/circle">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="75sp"
        android:layout_marginTop="-20dp"
        android:background="@android:color/transparent"
        android:text="+"
        android:textColor="#00ff00"
        android:paddingTop="0dp"
        android:layout_centerVertical="true"/>

</RelativeLayout>

在此处输入图片说明

尝试将填充顶部添加为0dp

    android:paddingTop="0dp"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM