Android: 按钮按下时颜色不变
Android: Button does not change color when pressed
我在我的 drawable
文件夹中创建了一个 custom_button.xml
,用于按下按钮时的默认设置。它看起来如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/red"
android:endColor="@color/light_red"
android:angle="270" />
<corners
android:radius="5dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="@color/light_purple"
android:endColor="@color/purple"
android:angle="270" />
<corners
android:radius="5dp" />
</shape>
</item>
我的按钮在未按下时看起来很好,它显示默认的紫色按钮。但是当我按下它时,它并没有像state_pressed
时那样变成红色按钮。
在我的activity_main.xml
中,按钮定义如下:
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/custom_button"
android:layout_margin="5dp"/>
我正在使用嵌套 LinearLayout
如果这有什么不同的话。
如果我遗漏了什么,请告诉我!
编辑: 我找到了问题的根源,但不确定如何解决。当我从我的 createListeners
中删除以下这些监听器时,按钮会按预期更改颜色。
b1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
int beat = getBeat(1);
m1 = MediaPlayer.create(MainActivity.this, beat);
m1.start();
m1.setLooping(true);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
m1.release();
}
return false;
}
});
它的基本功能是只要按下按钮就会播放一首歌曲。这如何影响按钮颜色?
我试过你的选择器,它对我有效。
我注意到你按钮的宽度是 0dp,什么时候
我用我这边的XML,没用。
尝试将其更改为:
<Button
android:id="@+id/button1"
android:layout_width="warap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="test"
android:background="@drawable/custom_button"
android:layout_margin="5dp"/>
这是我使用的custom_buttom.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#FF0000"
android:endColor="#FF00FF"
android:angle="270" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#1E669B"
android:endColor="#1E669B"
android:angle="270" />
</shape>
</item>
</selector>
好吧,我已经设法让它工作了,但这可能是一种迂回的方式。我基本上改变了 setOnTouchListener
所以它看起来像这样:
b1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
int beat = getBeat(1);
m1 = MediaPlayer.create(MainActivity.this, beat);
m1.start();
m1.setLooping(true);
b1.setPressed(true);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
b1.setPressed(false);
m1.release();
}
return false;
}
});
只是手动改变按钮的按下状态。
onTouchListener()
似乎无法与选择器一起正常工作。您必须在 ACTION_DOWN 和 ACTION_UP 中手动更改背景。
我在我的 drawable
文件夹中创建了一个 custom_button.xml
,用于按下按钮时的默认设置。它看起来如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/red"
android:endColor="@color/light_red"
android:angle="270" />
<corners
android:radius="5dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="@color/light_purple"
android:endColor="@color/purple"
android:angle="270" />
<corners
android:radius="5dp" />
</shape>
</item>
我的按钮在未按下时看起来很好,它显示默认的紫色按钮。但是当我按下它时,它并没有像state_pressed
时那样变成红色按钮。
在我的activity_main.xml
中,按钮定义如下:
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/custom_button"
android:layout_margin="5dp"/>
我正在使用嵌套 LinearLayout
如果这有什么不同的话。
如果我遗漏了什么,请告诉我!
编辑: 我找到了问题的根源,但不确定如何解决。当我从我的 createListeners
中删除以下这些监听器时,按钮会按预期更改颜色。
b1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
int beat = getBeat(1);
m1 = MediaPlayer.create(MainActivity.this, beat);
m1.start();
m1.setLooping(true);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
m1.release();
}
return false;
}
});
它的基本功能是只要按下按钮就会播放一首歌曲。这如何影响按钮颜色?
我试过你的选择器,它对我有效。 我注意到你按钮的宽度是 0dp,什么时候 我用我这边的XML,没用。
尝试将其更改为:
<Button
android:id="@+id/button1"
android:layout_width="warap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="test"
android:background="@drawable/custom_button"
android:layout_margin="5dp"/>
这是我使用的custom_buttom.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#FF0000"
android:endColor="#FF00FF"
android:angle="270" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#1E669B"
android:endColor="#1E669B"
android:angle="270" />
</shape>
</item>
</selector>
好吧,我已经设法让它工作了,但这可能是一种迂回的方式。我基本上改变了 setOnTouchListener
所以它看起来像这样:
b1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
int beat = getBeat(1);
m1 = MediaPlayer.create(MainActivity.this, beat);
m1.start();
m1.setLooping(true);
b1.setPressed(true);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
b1.setPressed(false);
m1.release();
}
return false;
}
});
只是手动改变按钮的按下状态。
onTouchListener()
似乎无法与选择器一起正常工作。您必须在 ACTION_DOWN 和 ACTION_UP 中手动更改背景。