【问题标题】:Android: text not being centered inside button/textview with fixed width/heightAndroid:文本不在按钮/文本视图中居中,宽度/高度固定
【发布时间】:2016-02-08 04:17:12
【问题描述】:

我在尝试做一些非常简单的事情时遇到了问题:在具有固定宽度和高度的按钮/TextView 中居中文本,当我增加其大小时,我的文本一直被向下推,这是我的代码示例正在使用:

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

这是我得到的结果图片:

有人能帮帮我吗?

谢谢!

【问题讨论】:

    标签: android button textview center


    【解决方案1】:

    你也可以这样用

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

    【讨论】:

    • 是的,我明白了,正如你所说,它不是那么“优雅”,其他解决方案似乎更简单 x)
    • 但是你为什么不用图标呢?
    • 图标是图像,加载它比简单的文本需要更多时间
    【解决方案2】:

    尝试将上边距添加为 0dp

        android:paddingTop="0dp"
    

    【讨论】:

    • 请尝试将 textSize 减小到 55sp
    • 似乎完全居中,谢谢!我想知道是否有另一种解决方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 2020-02-14
    • 2010-12-16
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多