如何在布局(android xml)中将unicode符号作为按钮内部的文本居中?

问题描述 投票:0回答:1

我正在为 Android 创建计算器。在我的layout.xml 文件中,我有

<LinearLayout>
<Button>
。我想要一个带有单符号文本的按钮,该按钮必须水平和垂直居中。我已经成功地将大多数文本符号(箭头除外)居中,其 Unicode 为
\u2190
和 '\u2192'。是否可以使用 paddings 或
android:gravity
来解决这个问题?或者有更复杂的解决方案?

在此屏幕截图中,您可以找到一个 example of incorrect arrows' positioning along with correct other symbols' positioning

我尝试了

android:textSize
、'android:paddingBottom' 和 'android:paddingTop' 的几种不同值的组合。我希望带有某些参数的简单“android:paddingBottom”应该对我有帮助,但如果我指定的值大于
10dp
,文本开始向上移动,而不是向下移动。

这是我的

layout.xml

<Button
   style="@style/ExtraButtonStyle"
   android:text="\u2190"
   android:textSize="44dp"
   android:paddingBottom="4dp"
   android:id="@+id/left_arrow">
</Button>

这是我的

styles.xml
文件,其中包含应用的样式:

<resources>
    <style name="MyButtonStyle">
        <item name="android:layout_width">60dp</item>
        <item name="android:layout_height">60dp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:layout_marginLeft">12dp</item>
        <item name="android:layout_marginRight">12dp</item>
        <item name="android:layout_marginTop">5dp</item>
        <item name="android:layout_marginBottom">5dp</item>
        <item name="android:textSize">30dp</item>
        <item name="android:textAllCaps">false</item>
    </style>

    <style name="ExtraButtonStyle" parent="MyButtonStyle">
        <item name="android:backgroundTint">@color/grey</item>
    </style>
</resources>
android android-layout button padding centering
1个回答
0
投票

删除

layout.xml
中的行

android:paddingBottom="4dp"

并在该行后面添加

MainActivity

Button buttonLeftArrow = findViewById(R.id.left_arrow);

附加线

buttonLeftArrow.setPadding(0,0,0,32);

© www.soinside.com 2019 - 2024. All rights reserved.