我正在尝试为按下的按钮设置不同的样式,但它不起作用。 (默认样式有效。)这是完整的代码。请你能看到这个问题。
Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="???"
android:background="@drawable/button_generic"/>
</RelativeLayout>
可绘制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"> <!--Android ignores this :-/ -->
<shape>
<solid android:color="#ff0000" />
<stroke android:width="2dp" android:color="#adadad" />
</shape>
</item>
<item>
<shape>
<solid android:color="#e8e8e8" />
<stroke android:width="1px" android:color="#adadad" />
</shape>
</item>
</selector>
Activity
public class TActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.t);
}
}
最佳答案
试试:
- 将这两个属性添加到 xml 中的
Button
:
android:clickable="true"
android:focusable="true"
- 或在
Activity
中以编程方式将View.OnClickListener
添加到您的Button
关于android - 按钮选择器 pressed_state 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49960308/