android - 自定义按钮的 "android:gravity"在 styles.xml 中定义时不适用

标签 android android-button android-styles android-gravity

我无法在 styles.xml 中定义的按钮中(垂直和水平)居中文本(当所有属性都在布局中时可以正常工作)

最初所有属性都是直接在布局中设置的,但我有很多 fragment ,其中所有按钮都是相同的。当 Button 在布局中完全声明时,我什至不需要指定“重力”值,文本会自动居中。

我尝试为某些属性创建样式,但我丢失了文本居中并且“android:gravity="center""在样式和/或布局中不起作用

styles.xml

<!-- GENERAL - BUTTON -->
    <style name="Widget.AppCompat.Button.one_line">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">50dp</item>
        <item name="android:paddingStart">20dp</item>
        <item name="android:paddingEnd">20dp</item>
        <item name="android:paddingBottom">20dp</item>
        <item name="android:fontFamily">@font/roboto_condensed_bold</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">@dimen/default_text_size</item>
        <item name="android:gravity">center_vertical</item>
    </style>

myLayout.xml

<android.support.constraint.ConstraintLayout>
...
    <Button
        android:id="@+id/btn_back"
        style="@style/Widget.AppCompat.Button.one_line"
        android:background="@drawable/border_green_full"
        android:text="@string/retour"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
<android.support.constraint.ConstraintLayout>

retour.png

我希望文本垂直居中。我错过了什么?

最佳答案

在您的情况下,只需删除:

<item name="android:paddingBottom">20dp</item>

最好使用parent 样式:

<style name="Widget.AppCompat.Button.one_line" parent="@style/Widget.AppCompat.Button">
 ...
</style>

这种情况下的默认值是:

<item name="android:gravity">center_vertical|center_horizontal</item>

一般来说:

Originally all attributes were set directly in the layout, but I have lots of fragments where all buttons are the same

如果您想在您的应用中集中所有按钮的样式避免在任何按钮中指定style属性布局只使用:

  • 使用 Material Theme 应用主题中的 materialButtonStyle 属性:

类似于:

<!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.MaterialComponents.Light">
     ...
    <item name="materialButtonStyle">@style/Widget.MaterialComponents.Button</item>
  </style>
  • 对于AppCompat 主题,使用buttonStyle 属性:

类似于:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="buttonStyle">@style/Widget.AppCompat.Button</item>
</style>

如果您想自定义样式,请使用 Widget.MaterialComponents.Button/Widget.AppCompat 定义 parent 样式.按钮.

关于android - 自定义按钮的 "android:gravity"在 styles.xml 中定义时不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57676461/

相关文章:

java - 在自定义 View 中应用内置的 Android 小部件样式

android - 如何循环多个按钮 View ?

android - 在操作栏中设置 android 搜索 View 和下拉列表的样式

Android Light AppTheme with Dark ActionBar (AppCompat)

Android Studio 非常缓慢和滞后

Android fragment 忽略按钮点击

android - 滑动布局中的按钮 SetOnClickListener NullPointerException

android - Paypal :如何设置和处理预先批准的付款?

android - 是否可以通过编程方式打开 android 4 上的移动热点?

android - Google+ 登录 onConnected 未调用