我无法弄清楚为什么我的按钮没有按压效果。 我想我的风格就是问题。 该按钮显示在由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>
答案 0 :(得分:1)
您可以使用selector
作为背景绘图,请参阅此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>
答案 1 :(得分: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 :(得分:0)
我已经设法创建一个圆角的按压效果,点击这样就会消失:
selector.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>
gradient.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>
colorbutton.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>
并在按钮i上设置:
<Button
android:background="@drawable/selector" />
感谢@Omkar的帮助。