Geo*_*rge 7 layout android alpha button
我想在一些按钮上引入"向下"风格.我创建了一个样式和一个状态列表.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/inactive_button_background" android:state_enabled="false"/>
<item android:drawable="@drawable/active_button_background" android:state_enabled="true"/>
<item android:drawable="@drawable/pressed_button_background" android:state_pressed="true"></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
问题是我希望能够在单击按钮时仅更改背景的alpha(我将具有可变背景,因此使用Alpha通道设置已售出的颜色不是解决方案).
我只想从声明性的xml中做到这一点(不想用布局的东西来规范我的代码).
问题是我不知道如何将这种alpha混合应用于xml drawable中的按钮.我很确定有一种方法.
我回答这个问题有点迟,但我只是通过使用View.onTouchListner更改alpha值来做到这一点
您可以通过以这种方式实现onTouch来实现此目的
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
v.setAlpha(0.5f);
break;
case MotionEvent.ACTION_UP:
v.setAlpha(1f);
default :
v.setAlpha(1f)
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我想你不能从drawable改变alpha值,所以你只能以这种方式做到这一点.
这完美地工作
yourView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.setAlpha(0.5f);
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
v.setAlpha(1f);
break;
}
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
dar*_*ten -1
您必须在代码中添加一些内容。
XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:onClick="dialogClicked" />
Run Code Online (Sandbox Code Playgroud)
爪哇
public void dialogClicked(final View view)
{
... set the alpha programatically ...
}
Run Code Online (Sandbox Code Playgroud)