【问题标题】:Black button action on android studioandroid studio上的黑色按钮操作
【发布时间】:2020-10-11 20:15:19
【问题描述】:

我在 android studio 上将按钮颜色更改为黑色,如下所示: 在colors.xml中:

<color name="colorBlack">#000000</color>

在styles.xml中:

 <style name="AppTheme.Button" parent="Base.Widget.AppCompat.Button">
        <item name="colorButtonNormal">@color/colorBlack</item>
    </style>

在 layout_frag.xml 中:

 <Button
   android:id="@+id/button"
   android:theme="@style/AppTheme.Button"
   android:layout_width="match_parent"
   android:layout_height="75dp"
   android:text="@string/hello_first_fragment"
   android:textColor="@color/design_default_color_background" />

我的黑色按钮可以正常工作,尽管按下按钮时的色调背景颜色操作不再起作用,因为按钮非常暗并且色调通常为深灰色。 ![Button2 onClick 变成灰色][1] 我想知道单击按钮时在哪里可以更改着色操作?

【问题讨论】:

    标签: android xml android-button android-theme material-components-android


    【解决方案1】:

    您可以使用以下命令覆盖 colorControlHighlight 属性:

    <androidx.appcompat.widget.AppCompatButton
        android:theme="@style/ThemeOverylay.Button"/>
    

    与:

    <style name="ThemeOverylay.Button">
        <item name="colorControlHighlight">@color/....</item>
        <item name="colorButtonNormal">@color/....</item>
    </style>
    

    如果您使用的是 Material Components 主题,您可以使用 MaterialButton rippleColor 属性:

    <com.google.android.material.button.MaterialButton
        app:rippleColor="@color/my_selector"
        ../>
    

    使用这样的选择器:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
      <item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_pressed="true"/>
      <item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_focused="true" android:state_hovered="true"/>
      <item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_focused="true"/>
      <item android:alpha="..." android:color="?attr/colorOnPrimary" android:state_hovered="true"/>
      <item android:alpha="..." android:color="?attr/colorOnPrimary"/>
    
    </selector>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      相关资源
      最近更新 更多