我正在开发Android应用程序。我想在屏幕底部横向放置4个按钮。其中2个按钮上有图像。按钮的边框颜色应为黑色,边框应尽可能细。当我单击按钮时,我希望按钮的背景色更改为蓝色,而不更改边框的颜色,并保持该颜色一段时间。如何在Android中实现这种情况?
我正在开发Android应用程序。我想在屏幕底部横向放置4个按钮。其中2个按钮上有图像。按钮的边框颜色应为黑色,边框应尽可能细。当我单击按钮时,我希望按钮的背景色更改为蓝色,而不更改边框的颜色,并保持该颜色一段时间。如何在Android中实现这种情况?
一种方法是在drawable
目录下创建一个名为whatever.xml的XML文件,内容如下:
<?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="true"
android:drawable="@drawable/bgalt" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/bgalt" />
<item android:drawable="@drawable/bgnorm" />
</selector>
bgalt
和bgnorm
是在drawable中的PNG图像。
如果您在活动中以编程方式创建按钮,您可以使用以下代码设置背景:
final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
如果您使用XML设置按钮的样式,您可以这样做:
<Button
android:id="@+id/mybutton"
android:background="@drawable/watever" />
最后,这里有一个指向教程的链接。
将此代码保存在drawable文件夹中,文件名为"bg_button.xml",并在您的xml文件中将"@drawable/bg_button"作为按钮的背景调用:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#004F81" />
<stroke
android:width="1dp"
android:color="#222222" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#89cbee"
android:endColor="#004F81"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#4aa5d4" />
<corners
android:radius="7dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
试一试
final Button button = (Button) findViewById(R.id.button_id);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
button.setBackgroundColor(Color.RED);
} else if(event.getAction() == MotionEvent.ACTION_DOWN) {
button.setBackgroundColor(Color.BLUE);
}
return false;
}
});
可以参考这个:
boolean check = false;
Button backward_img;
Button backward_img1;
backward_img = (Button) findViewById(R.id.bars_footer_backward);
backward_img1 = (Button) findViewById(R.id.bars_footer_backward1);
backward_img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
check = true;
backward_img.setBackgroundColor(Color.BLUE);
}
});
if (check == true) {
backward_img1.setBackgroundColor(Color.RED);
backward_img.setBackgroundColor(Color.BLUE);
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- default -->
<item
android:state_pressed="false"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#00a3e2" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
<!-- button focused -->
<item
android:state_pressed="false"
android:state_focused="true">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#5a97f5" />
<padding
android:bottom="5dp"
android:left="10dp"
android:right="10dp"
android:top="5dp" />
</shape></item>
<!-- button pressed -->
<item
android:state_pressed="true"
android:state_focused="false">
<shape
android:innerRadiusRatio="1"
android:shape="rectangle" >
<solid android:color="#478df9"/>
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape></item>
</selector>
1-制作一个按钮形状:右键单击drawable,新建drawable资源文件。将根元素更改为shape并创建您的形状。
2-现在复制您的形状并更改名称和填充颜色。 点击此处查看图片描述
3-右键单击drawable,新建drawable资源文件,将根元素设置为selector, 进入文件并设置“state_pressed”状态。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"android:drawable="@drawable/YourShape1"/>
<item android:state_pressed="false" android:drawable="@drawable/YourShape2"/>
</selector>
4-最后,转到XML布局并设置您的按钮背景“your selector”
(对不起我的英语不好)
<!-- Create new xml file like mybtn_layout.xml file in drawable -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed" /> <!--pressed -->
<item android:drawable="@drawable/normal" /> <!-- Normal -->
</selector>
<!-- Now this file should be in a drawable folder and use this
single line code in button code to get all the properties of this xml file -->
<Button
android:id="@+id/street_btn"
android:layout_width="wrap_content"
android:background="@drawable/layout_a" > <!-- your required code -->
</Button>
请尝试以下操作......
首先创建一个名为button_pressed.xml的xml文件,其内容如下:
<?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="@drawable/icon_1" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/icon_1_press" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/icon_1_press" />
<item android:drawable="@drawable/icon_1" />
</selector>
int imgID = getResources().getIdentifier("button_pressed", "drawable", getApplication().getPackageName());
button.setImageResource(imgID);
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="@drawable/login" /> <!-- default -->
</selector>
在drawable文件夹中编写此代码,创建一个新的资源并将其命名为您想要的名称,然后在按钮中写入此可绘制项的名称,就像我们在Android中引用图像SRC一样。
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/color_gray">
<item android:id="@android:id/mask">
<color android:color="@color/color_gray" />
</item></ripple>