现在的位置: 首页 > 综合 > 正文

android–widget介绍之ImageButton

2017年12月03日 ⁄ 综合 ⁄ 共 1844字 ⁄ 字号 评论关闭
android--widget介绍之ImageButton
在UI设计中,Button是一个常用控件,但是Button太普通,不够艺术化,和页面的其他元素不协调,这时,往往会希望用图片代替,ImageButton由此产生。

如下是一个完整的demo,包含了click事件处理

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout 
	    android:orientation="vertical"
	    android:layout_width="wrap_content"
	    android:layout_height="fill_parent"
	    >
	    <Button android:text="ok1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
	    <ImageButton android:src="@drawable/lock1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/imageButton1"></ImageButton>
	</LinearLayout>
    <ImageButton android:src="@drawable/lock2" android:layout_height="wrap_content" android:layout_width="wrap_content" 
    		android:id="@+id/imageButton2"  android:layout_marginTop="30px"></ImageButton>
</LinearLayout>

activity代码:

public class Ex02Activity extends Activity {
	
	private Button btnOk;
	private ImageButton btnCc;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnOk = (Button) findViewById(R.id.button1);
        btnCc = (ImageButton) findViewById(R.id.imageButton1);
        
        btnOk.setBackgroundColor(Color.BLUE);
        btnOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setTitle("this is btnOk");
			}
		});
        btnCc.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(Ex02Activity.this, "BMI 計算器", Toast.LENGTH_SHORT).show();
				/*new AlertDialog.Builder(Ex02Activity.this)
				.setTitle("關於 Android BMI")
				.setMessage("Android BMI Calc")
				.setPositiveButton("ok", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				})
				.show();*/
        	}
		});
    }

说明: android有ImageButton控件,只需附上图片的来源即可。

抱歉!评论已关闭.