【问题标题】:Button with background and shape doesn't have a press effect带背景和形状的按钮没有按下效果
【发布时间】:2018-11-12 14:35:40
【问题描述】:

我不明白为什么我的按钮没有按下效果。 我想我的风格是问题所在。 该按钮显示在 AppCombatActivity 托管的片段内

v21 style.xml 是

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>

style.xml 是

  <style name="MyTheme" parent="MyTheme.Base">
  </style>
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Your customizations go here -->
    <item name="buttonStyle">@style/LoginButton</item>
    <!-- Override the color of UI controls -->
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:textColorSecondary">@color/white</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="colorPrimary">@color/my_blue</item>
    <item name="colorPrimaryDark">@color/my_blue</item>
    <item name="colorAccent">@color/my_purple</item>
  </style>
  <style name="LoginButton" parent="Widget.AppCompat.Button.Colored">
    <item name="android:textAllCaps">false</item>
    <item name="colorControlHighlight">@color/my_purple</item>
    <item name="android:textColor">@color/white</item>
  </style>

按钮代码:

<Button
      android:theme="@style/LoginButton"
        android:background="@drawable/roundedbutton_do_this"
        android:backgroundTint="@color/dark_blue"
        android:id="@+id/SelectDateButton"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:gravity="center"
        android:text="@string/ChooseDateTime_SelectDate"
        android:layout_margin="12dp"
        android:singleLine="false"
        android:layout_gravity="center_vertical" />

背景代码:

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
  <solid android:color="@color/green"/>
  <corners android:radius="10dp" />
</shape>

minSdkVersion:16

targetSdkVersion:27

现在我在按下按钮时看不到任何效果。在 android 6 和 7 上测试它

编辑:我试过这个,但按钮没有形状(圆角)

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_focused="true" android:state_pressed="false" android:drawable="@color/green" />
  <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
  <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
  <item android:drawable="@color/green" />
  <item>
    <shape android:shape="rectangle">
      <solid android:color="@color/green"/>
      <corners android:radius="10dp" />
    </shape>
  </item>
</selector>

【问题讨论】:

    标签: android android-layout button material-design android-button


    【解决方案1】:
     <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:clickable="true"
                    android:focusable="true"
                    app:cardCornerRadius="@dimen/_3sdp"
                    app:cardBackgroundColor="@android:color/white">
    

    【讨论】:

      【解决方案2】:

      你可以使用selector作为背景drawable,参考这个answer

      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:state_focused="true" android:state_pressed="false" android:drawable="@color/green" />
          <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/ANY_OTHER" />
          <item android:state_focused="false" android:state_pressed="true" android:drawable="@color/ANY_OTHER" />
          <item android:drawable="@color/green" />
      </selector>
      

      【讨论】:

      • 在这个答案中,他使用图像作为按钮。我只使用颜色,我怎么能这样做?
      • @CDrosos 您为按下的颜色制作了一种颜色,为未按下的颜色制作了一种颜色。但是选择器背景可绘制是新闻效果的工作方式。
      • @CDrosos 用color 更新了答案,例如用你想要的按下状态color 替换@color/ANY_OTHER
      • @Omkar 形状怎么样?我已经用我尝试过的代码更新了问题,现在按钮有一个小的 OnPress 效果,但形状不起作用。按钮不圆
      • 直接将其用作可绘制对象@drawable/background 而不是@color/ANY_OTHER 可以使用任何一种方式。
      【解决方案3】:

      我已经设法创建了一个带有圆角的新闻效果,点击这种方式会消失:

      选择器.xml

      <?xml version="1.0" encoding="utf-8" ?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">
        <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/colorbutton" />
        <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
        <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
        <item android:drawable="@drawable/colorbutton" />
      </selector>
      

      渐变.xml

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
          <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
            <corners android:radius="10dp" />
            <gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
          </shape>
        </item>
      </layer-list>
      

      颜色按钮.xml

      <?xml version="1.0" encoding="utf-8" ?>
          <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="@color/green"/>
            <corners android:radius="10dp" />
          </shape>
      

      在我设置的按钮上:

      <Button
      android:background="@drawable/selector" />
      

      感谢@Omkar 的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 2012-07-03
        • 2021-05-22
        • 1970-01-01
        • 1970-01-01
        • 2014-10-20
        相关资源
        最近更新 更多