Android 安卓:更改背景色视图点击按钮
当我单击Android 安卓:更改背景色视图点击按钮,android,view,colors,background,Android,View,Colors,Background,当我单击按钮时,如何将背景色更改为按钮下的视图?我尝试了一个选择器不起作用,因为视图没有改变颜色。有什么问题 这是我的代码: XML ... <View android:id="@+id/viewPlanos2" android:layout_width="match_parent" android:layout_height="3dp" android:layout
按钮时,如何将背景色更改为按钮下的视图
?我尝试了一个选择器
不起作用,因为视图没有改变颜色。有什么问题
这是我的代码:
XML
...
<View
android:id="@+id/viewPlanos2"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_gravity="center" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@color/transparente"
android:drawableTop="@drawable/buttonimage"
android:gravity="center"
android:paddingTop="50dp" />
<View
android:id="@+id/viewPlanos1"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_gravity="center" />
...
linea_verde
但是代码不起作用您错误地使用了drawable,请使用此代码
view.setBackgroundResource(R.drawable.linea_verde)
正如您所说,当我单击按钮时,将背景颜色更改为按钮下的视图
你喜欢这样吗
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// change color of your view here
linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));
}
});
setBackgroundColor()
仅适用于颜色,但您使用的状态列表似乎是可绘制的。如果确定要根据按钮的状态使用不同的颜色,请使用以下代码设置可绘制的状态列表:
view.setBackgroundDrawable(R.drawable.linea_verde);
否则,只需使用
view.setBackgroundColor(getResources().getColor(R.drawable.yourcolor);
但在这种情况下,请使用单击侦听器,并确保实际使用颜色资源,例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
#f00
#80ff0000
您也可以这样做
android:background="@drawable/linea_verde"
如果您想应用所选项目的默认背景,可以将background
属性与android?attr
一起使用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground" />
我认为你用错误的方式比较了这个观点。我碰巧遇到了这个问题,因为它没有标记为已回答,这可能会帮助其他人。为您的形状创建两个单独的文件,并在单击按钮后更改视图的背景:
private boolean isPressed = false;
public void onClick(View v) {
if(v == botonMetro) {
if(isPressed){
linea2.setBackgroundResource(R.drawable.pressed_shape);
} else{
linea2.setBackgroundResource(R.drawable.un_pressed_shape);
}
isPressed = !isPressed;
}
}
在onCreate()
中直接使用setBackgroundDrawable。
android:background="@drawable/linea_verde"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground" />
<item name="selectableItemBackground">@drawable/abc_item_background_holo_dark</item>
public void onClick(View v) {
int id = v.getId();
if(id == R.id.button) {
linea2.setBackgroundResource(R.drawable.linea_verde);
}
}
private boolean isPressed = false;
public void onClick(View v) {
if(v == botonMetro) {
if(isPressed){
linea2.setBackgroundResource(R.drawable.pressed_shape);
} else{
linea2.setBackgroundResource(R.drawable.un_pressed_shape);
}
isPressed = !isPressed;
}
}