深色主题中的 TextButton 默认颜色

发布于 2025-01-18 05:13:34 字数 1361 浏览 0 评论 0原文

尝试更改我的 Android 应用程序中所有 TextButtons 的默认颜色和材质。我看到我可以设置“materialButtonStyle”和“materialButtonOutlinedStyle”的样式。但是,我没有看到文本按钮的属性,可能类似于“materialButtonTextStyle

<style name="AppTheme" parent="Theme.MaterialComponents">
  <item name="materialButtonStyle">@style/myButton</item>
  <item name="materialButtonOutlinedStyle">@style/myButtonOutlined</item>
</style>

示例按钮:

<Button
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Widget.MaterialComponents.Button.TextButton"
    android:text="Text Button" />

是否可以全局更改所有材质文本按钮的样式?

编辑:从主题按钮材质文档中尝试过此操作。

<style name="AppTheme" parent="Theme.MaterialComponents">
  <item name="borderlessButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>

<style name="Widget.App.Button.TextButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.TextButton</item>
</style>

<style name="ThemeOverlay.App.Button.TextButton" parent="">
    <item name="colorPrimary">@color/md_purple_600</item>
</style>

但是仍然没有为所有文本按钮设置文本颜色样式。

Trying to change the default color for all TextButtons in my Android app w/ material. I see that I can style a "materialButtonStyle" and "materialButtonOutlinedStyle". However I do not see an attribute for a text button, possible something like "materialButtonTextStyle"

<style name="AppTheme" parent="Theme.MaterialComponents">
  <item name="materialButtonStyle">@style/myButton</item>
  <item name="materialButtonOutlinedStyle">@style/myButtonOutlined</item>
</style>

Example button:

<Button
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Widget.MaterialComponents.Button.TextButton"
    android:text="Text Button" />

Is it possible to change the style globally for all material text buttons?

EDIT: Tried this from the theming-buttons material docs.

<style name="AppTheme" parent="Theme.MaterialComponents">
  <item name="borderlessButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>

<style name="Widget.App.Button.TextButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.TextButton</item>
</style>

<style name="ThemeOverlay.App.Button.TextButton" parent="">
    <item name="colorPrimary">@color/md_purple_600</item>
</style>

However still not styling the text color for all text buttons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩起发的微风 2025-01-25 05:13:34

根据 buttons -baterons -material Design 文档文档textbutton默认样式主题属性?atter/borderlessbuttonStyle可以在Apptheme中使用该样式在所有材料文本按钮上全球更改样式。

要为所有文本键的全球更改文本颜色:

1。定义borderlessButtonStyle在灯光和夜晚中的属性如下:

<!-- Base application theme. -->
<style name="MyAppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
  <item name="borderlessButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>

<!-- For TextButton-->
<style name="Widget.App.Button.TextButton" parent="Widget.MaterialComponents.Button.TextButton">
  <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.TextButton</item>
</style>

<style name="ThemeOverlay.App.Button.TextButton" parent="">
  <item name="colorPrimary">@android:color/holo_red_dark</item>
</style> 

2。在每个TextButton xml xml布局中定义样式style = style =“? attr/borderlessbuttonStyle“如下:

<Button
    style="?attr/borderlessButtonStyle"
    android:id="@+id/text_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text Button" />

和文本颜色将在全球更改为colorprimary,该在光/夜主题中定义。

您可以为其他材料按钮样式实现相同

<!-- Base application theme. -->
<style name="MyAppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="borderlessButtonStyle">@style/Widget.App.Button.TextButton</item>
    <item name="materialButtonOutlinedStyle">@style/Widget.App.Button.OutlinedButton</item>
    <item name="materialButtonStyle">@style/Widget.App.Button</item>
</style>


<!-- For TextButton-->
<style name="Widget.App.Button.TextButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.TextButton</item>
</style>

<style name="ThemeOverlay.App.Button.TextButton" parent="">
    <item name="colorPrimary">@android:color/holo_red_dark</item>
</style>

<!-- For OutlinedButton-->
<style name="Widget.App.Button.OutlinedButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.OutlinedButton</item>
</style>

<style name="ThemeOverlay.App.Button.OutlinedButton" parent="">
    <item name="colorPrimary">@android:color/holo_orange_dark</item>
</style>

<!-- For Button -->
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
</style>

<style name="ThemeOverlay.App.Button" parent="">
    <item name="colorPrimary">@android:color/holo_blue_dark</item>
    <item name="colorOnPrimary">@android:color/holo_blue_bright</item>
</style>

按照相同的方式, 上方全局属性:

<Button
    style="?attr/borderlessButtonStyle"
    android:id="@+id/text_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text Button" />

<Button
    style="?attr/materialButtonOutlinedStyle"
    android:id="@+id/outlined_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Outlined Button" />

<Button
    android:id="@+id/default_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Default Button" />

下面是光模式下每个按钮样式的示例结果:

“

及以下是夜间模式下每个按钮样式的示例结果:

“

As per the Buttons -Material design documentation the TextButton has a default style theme attribute ?attr/borderlessButtonStyle which can be used in the AppTheme to change the style globally for all material text buttons.

To change the Text Colour globally for all TextButtons:

1.Define the borderlessButtonStyle attribute in the Light and Night AppTheme like the below:

<!-- Base application theme. -->
<style name="MyAppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
  <item name="borderlessButtonStyle">@style/Widget.App.Button.TextButton</item>
</style>

<!-- For TextButton-->
<style name="Widget.App.Button.TextButton" parent="Widget.MaterialComponents.Button.TextButton">
  <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.TextButton</item>
</style>

<style name="ThemeOverlay.App.Button.TextButton" parent="">
  <item name="colorPrimary">@android:color/holo_red_dark</item>
</style> 

2.In every TextButton xml layout define the style style="?attr/borderlessButtonStyle" like the below:

<Button
    style="?attr/borderlessButtonStyle"
    android:id="@+id/text_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text Button" />

And the Text colour will be changed globally based to the colorPrimary which is defined in Light/Night Theme.

Following the same way you can achieve the same for the other Material Button Styles like OutlinedButton and the default Button like:

<!-- Base application theme. -->
<style name="MyAppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <item name="borderlessButtonStyle">@style/Widget.App.Button.TextButton</item>
    <item name="materialButtonOutlinedStyle">@style/Widget.App.Button.OutlinedButton</item>
    <item name="materialButtonStyle">@style/Widget.App.Button</item>
</style>


<!-- For TextButton-->
<style name="Widget.App.Button.TextButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.TextButton</item>
</style>

<style name="ThemeOverlay.App.Button.TextButton" parent="">
    <item name="colorPrimary">@android:color/holo_red_dark</item>
</style>

<!-- For OutlinedButton-->
<style name="Widget.App.Button.OutlinedButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button.OutlinedButton</item>
</style>

<style name="ThemeOverlay.App.Button.OutlinedButton" parent="">
    <item name="colorPrimary">@android:color/holo_orange_dark</item>
</style>

<!-- For Button -->
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
    <item name="materialThemeOverlay">@style/ThemeOverlay.App.Button</item>
</style>

<style name="ThemeOverlay.App.Button" parent="">
    <item name="colorPrimary">@android:color/holo_blue_dark</item>
    <item name="colorOnPrimary">@android:color/holo_blue_bright</item>
</style>

And each Button listens to the above global attributes:

<Button
    style="?attr/borderlessButtonStyle"
    android:id="@+id/text_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text Button" />

<Button
    style="?attr/materialButtonOutlinedStyle"
    android:id="@+id/outlined_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Outlined Button" />

<Button
    android:id="@+id/default_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Default Button" />

Below is a sample result for each Button Style in Light mode:

light_mode

And below is a sample result for each Button Style in Night mode:

dark_mode

甜嗑 2025-01-25 05:13:34

如您所见,从 textbuttoncolors 使用材料的主要颜色作为默认内容颜色。

@Composable
fun textButtonColors(
    backgroundColor: Color = Color.Transparent,
    contentColor: Color = MaterialTheme.colors.primary,
    disabledContentColor: Color = MaterialTheme.colors.onSurface
        .copy(alpha = ContentAlpha.disabled)
): ButtonColors = DefaultButtonColors(
    backgroundColor = backgroundColor,
    contentColor = contentColor,
    disabledBackgroundColor = backgroundColor,
    disabledContentColor = disabledContentColor
)

为了实现目标,如果您不更改原色,则可以定义自定义主题并覆盖文本按钮的样式。

@Composable
fun CustomTextButtonStyle(content: @Composable () -> Unit) {
    MaterialTheme(
        colors = MaterialTheme.colors.copy(
            primary = Color.Green // Set your custom color
        )
    ) {
        content()
    }
}
CustomTextButtonStyle() {
    TextButton(onClick = { }) {
        Text(text = "Hello")
    }
}

As you can see from textButtonColors, TextButton use the primary color of your MaterialTheme as default content color.

@Composable
fun textButtonColors(
    backgroundColor: Color = Color.Transparent,
    contentColor: Color = MaterialTheme.colors.primary,
    disabledContentColor: Color = MaterialTheme.colors.onSurface
        .copy(alpha = ContentAlpha.disabled)
): ButtonColors = DefaultButtonColors(
    backgroundColor = backgroundColor,
    contentColor = contentColor,
    disabledBackgroundColor = backgroundColor,
    disabledContentColor = disabledContentColor
)

To achieve your goal, if you don't change your primary color, you can define a custom theme and override the style of your text buttons.

@Composable
fun CustomTextButtonStyle(content: @Composable () -> Unit) {
    MaterialTheme(
        colors = MaterialTheme.colors.copy(
            primary = Color.Green // Set your custom color
        )
    ) {
        content()
    }
}
CustomTextButtonStyle() {
    TextButton(onClick = { }) {
        Text(text = "Hello")
    }
}
忘羡 2025-01-25 05:13:34

在标题中,它说您想为黑暗主题定义 textcolor 。您是否在value-night中定义了主题,因此如果手机处于黑暗模式,您的应用程序将采用这些值。

In the title it says you want to define textColor for a dark theme. Did you define the theme in values-night so your app will take these values if the phone is in dark mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文