androidbuttonimage
❶ android中带图标的按钮(ImageButton)怎么用
除了Android系统自带的Button按钮以外,还提供了带图标的按钮ImageButton
要制作带图标的按钮,首先要在布局文件中定义ImageButton,然后通过setImageDrawable方法来设置要显示的图标。
注意:
我们可以在布局文件中就直接设置按钮的图标,如
android:src=”@drawable/icon1″
我们也可以在程序中设置自定义图标
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
我们还可以使用系统自带的图标
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));
设置完按钮的图标后,需要为按钮设置监听setOnClickListener,以此捕获事件并处理
下面的例子讲述的是由4个图标按钮组成的布局,其中三个按钮的图标是自定义的,第四个按钮的图标是系统的,当点击按钮1的时候,弹出dialog,当点击按钮2的时候,点击确定后,可以将按钮2的图标变成按钮3的图标,当点击按钮3的时候,按钮3的图标变成了系统打电话的图标,点击按钮4,显示一个提示dialog
ImageButtonTest.java源代码
package org.loulijun.imagebutton;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class ImageButtonTest extends Activity {
/** Called when the activity is first created. */
TextView textview;
ImageButton imgbtn1;
ImageButton imgbtn2;
ImageButton imgbtn3;
ImageButton imgbtn4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview=(TextView)findViewById(R.id.textview);
//分别取得4个ImageButton对象
imgbtn1=(ImageButton)findViewById(R.id.imagebutton1);
imgbtn2=(ImageButton)findViewById(R.id.imagebutton2);
imgbtn3=(ImageButton)findViewById(R.id.imagebutton3);
imgbtn4=(ImageButton)findViewById(R.id.imagebutton4);
//分别为ImageButton设置图标
//imgbtn1已经在main.xml布局中设置了图标,所以就不在这里设置了(设置图标即可在程序中设置,也可在布局文件中设置)
imgbtn2.setImageDrawable(getResources().getDrawable(R.drawable.icon));//在程序中设置图标
imgbtn3.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
imgbtn4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));//设置系统图标
//下面为各个按钮设置事件监听
imgbtn1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton1")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//相应的处理操作
}
}).create();
dialog.show();
}
});
imgbtn2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton2,我要使用ImageButton3的图标")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
imgbtn2.setImageDrawable(getResources().getDrawable(R.drawable.icon2));
}
}).create();
dialog.show();
}
});
imgbtn3.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是ImageButton3,我想使用系统打电话的图标")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
imgbtn3.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_action_call));
}
}).create();
dialog.show();
}
});
imgbtn4.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(ImageButtonTest.this)
.setTitle("提示")
.setMessage("我是使用的系统图标")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//相应的处理操作
}
}).create();
dialog.show();
}
});
}
}
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ImageButton测试案例"
/>
<ImageButton
android:id="@+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon1"
/>
<ImageButton
android:id="@+id/imagebutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/imagebutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="@+id/imagebutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
❷ Android中的Button怎么在点击更换背景点击后又恢复原来的背景
只需要给Button配置一个Selector背景选择器即可实现。
1.创建mylist_view.xml文件
首先在res目录下新建drawable文件夹,再在新建的drawable文件夹中新建mylist_view.xml,其目录结构为:res/drawable/mylist_view.xml。
2.根据具体需求编辑mylist_view.xml文件
新建mylist_view.xml文件后,在没有添加任何属性时其内部代码结构为:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>
3.下面就可以根据项目需求,在其内部定义为自己想要的样式了,主要属性如下:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认时的背景图片-->
<item android:drawable="@drawable/pic1" />
<!-- 没有焦点时的背景图片 -->
<item android:state_window_focused="false" android:drawable="@drawable/pic1" />
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item android:state_focused="true" android:state_pressed="true" android:drawable= "@drawable/pic2" />
<!-- 触摸模式下单击时的背景图片-->
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pic3" />
<!--选中时的图片背景-->
<item android:state_selected="true" android:drawable="@drawable/pic4" />
<!--获得焦点时的图片背景-->
<item android:state_focused="true" android:drawable="@drawable/pic5" />
</selector>
4.引用mylist_view.xml文件
android:background="@drawable/mylist_view"
❸ android中,如何做圆形的button按钮
自己绘制圆形的图片,然后在button布局里面用BackgroundDrawable设置为button背景。android中是不带圆形的button的
❹ Android中如何判断两个Button的背景图片是否相等
android 根据button不同状态显示不同(背景)图片
2011-02-24 | nedvedno1 | 转藏(19)
网上的思路不错,我只做了基于xml的实现。先来贴一段网上的经典code:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/button_add" />
<item android:state_pressed="true" android:drawable="@drawable/button_add_pressed" />
<item android:state_focused="true" android:drawable="@drawable/button_add_pressed" />
<item android:drawable="@drawable/button_add" />
</selector>
这个文件放在drawable目录下面。命名为button_add_x.xml
使用的时候
<ImageButton
android:id="@+id/ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:src="@drawable/button_add_x"
>
</ImageButton>
按照这种方法 我试了多次,发现根本不能生效。后来才发现,这方法是针对ImageButton的,于是查了一下Button和ImageButton的区别,如果区别不大,我就打算把我自己的Button换成ImageButton了,但结果令我失望了,区别还不小呢,主要是这2种控件对于实现onClick方法的机制不同,想想就复杂,如果贸然换了,编译的时候恐怕要出现很多error。
再着,这方法并没有改变button的background,而我们基于button去实现图片一般都是直接+background的,对么?
下面贴我的code:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@drawable/xxx1" />
<item android:state_pressed="true" android:drawable="@drawable/xxx2" />
<item android:state_focused="true" android:drawable="@drawable/xxx3" />
<-- 这里还可以加N多效果和动作 只要你用的到 -->
<item android:drawable="@drawable/xxx4" />
</selector>这个文件没有不同,起名为button_add_x.xml 放在drawable下。
使用的时候有所不同
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_add_x" <---这里background直接指向刚刚编写的文件
//这行不需要--->android:src="@drawable/button_add_x"
>
</Button>ok,编译 push后就生效了。如果你原来的background指向的是一张.png图片的话,那么把它换成一个自己制作的文件就好。
❺ android 按钮设置点击更换imagebutton背景
button.setBackgroundResource(R.drawable.beijing1);
上面是改变按钮背景的代码
可以做两组图片,分别为button1的选中和为选择状态、button2的选中和为选择状态,让后再button1和button2的点击事件中,对两个按钮的背景进行改变,就可以了
❻ android点击以后改变button的颜色
android点击以后改变button的颜色的方法为:
1、新建 drawable/button_font_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff"/>
<item android:state_focused="true" android:color="#fff"/>
<item android:color="#000" />
</selector>
2、添加至需要的xml里即可
<android:textColor="@drawable/button_font_style">
❼ android中button有几种状态
Android中,button按钮通常有三个状态:
1. normal(正常状态);
2. focus(焦点状态);
3. pressed(按下状态)
4. selected(选中状态)
注意:按下后未松开前是pressed,表示按下。
松开后当前项目获得焦点,是focused。
focused的项只有一个,selected是当选中该按钮时显示的状态。
❽ Android 当我点击一个Button后,当前的Activity背景改变(颜色或者图片),应该怎么写
直接设置斗运资源文件:button.setBackground(R.id.image);
设置空伏梁drawable作厅野为背景button.setBackgroundDrawable(Drawable drawable);
❾ 在android中按钮共分为几种
从控件来说分为2种:button(一般按钮)和ImageButton(图片按钮);
但是大部分时候,开发者是可以通过各种方式自定义按钮,这样的话,界面呈现出来的按钮是多种多样的;
TextView,view等等,很多控件其实都可以拿来当按钮使用;
此外,还有包括ToggleButton,单选按钮,多选按钮等这些都属于是功能比较专一的特殊按钮了;
我想你只有对android比较了解的情况下,才可能理解深一些吧!
❿ android里面button的背景图片大小怎么能根据button大小缩放
<p>可以在ps中编辑到合适的分辨率
</p><p>可以把Button组件和一个ImageView组件放在一个FrameLayout布局中,规定好FrameLayout的尺寸,然后对上面的两个组件的宽和高都设置为match_parent</p><p>可以把上述两个组件换成一个ImageButton组件</p><p>如果需要图片的切换,可以考虑ImageSwitcher组件</p><p>图片缩放算法,缩放好后,动态为ImageView添加图片
</p><p>
</p>