1. 程式人生 > >Android 開發筆記___圖像按鈕__imageButton

Android 開發筆記___圖像按鈕__imageButton

view text 外觀 source 上下左右 sch pre void ica

IMAGEBUTTON

其實派生自image view,而不是派生自button。,image view擁有的屬性和方法,image button 統統擁有,只是imagebutton有個默認的按鈕外觀。

  • image button 只能顯示圖形
  • imagebutton 上面的圖片可按比例拉伸
  • 只能在背景顯示一張圖形,但分別在前景和背景顯示兩張圖片,實現圖片疊加的效果

  • 在輸入法無法輸入的字符和特殊字體顯示的字符串,就適合用imagebutton, 先切圖再顯示

  • 要想在文字周圍放圖片可以用基於text view 的 button,
  • 具體在xml中設置如下屬性:
    • drawableTop:指定文本上方的圖片
    • drawableBottom:指定文本下方的圖片
    • drawableLeft:指定文本左邊的圖形  
    • drawableRight:指定文本右邊的圖片
    • drawablepadding:指定圖形文本間距
  • 在代碼中:
    • setCompoundDrawables:設置文本周圍圖形,上下左右
    • setCompoundDrawablePadding:間距

技術分享

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <Button 8 android:id="@+id/btn_icon" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_marginTop
="10dp" 12 android:layout_gravity="center" 13 android:drawableLeft="@mipmap/ic_launcher" 14 android:drawablePadding="5dp" 15 android:text="熱烈歡迎" 16 android:textColor="#000000" 17 android:textSize="17sp" /> 18 19 <LinearLayout 20 android:layout_width="match_parent" 21 android:layout_height="wrap_content" 22 android:layout_marginTop="10dp" 23 android:orientation="horizontal"> 24 25 <Button 26 android:id="@+id/btn_left" 27 android:layout_width="0dp" 28 android:layout_height="wrap_content" 29 android:layout_weight="1" 30 android:text="圖標在左" 31 android:textColor="#000000" 32 android:textSize="15sp" /> 33 34 <Button 35 android:id="@+id/btn_top" 36 android:layout_width="0dp" 37 android:layout_height="wrap_content" 38 android:layout_weight="1" 39 android:text="圖標在上" 40 android:textColor="#000000" 41 android:textSize="15sp" /> 42 43 <Button 44 android:id="@+id/btn_right" 45 android:layout_width="0dp" 46 android:layout_height="wrap_content" 47 android:layout_weight="1" 48 android:text="圖標在右" 49 android:textColor="#000000" 50 android:textSize="15sp" /> 51 52 <Button 53 android:id="@+id/btn_bottom" 54 android:layout_width="0dp" 55 android:layout_height="wrap_content" 56 android:layout_weight="1" 57 android:text="圖標在下" 58 android:textColor="#000000" 59 android:textSize="15sp" /> 60 61 </LinearLayout> 62 63 </LinearLayout>

java

 1 package com.example.alimjan.hello_world;
 2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.graphics.drawable.Drawable;
 6 import android.os.Bundle;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.view.View;
 9 import android.widget.Button;
10 
11 /**
12  * Created by alimjan on 7/1/2017.
13  */
14 
15 
16 public class class__2_3_4 extends AppCompatActivity implements View.OnClickListener {
17     private Button btn_icon;
18     private Drawable drawable;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.code_2_3_4);
24         btn_icon = (Button) findViewById(R.id.btn_icon);
25         drawable = getResources().getDrawable(R.mipmap.ic_launcher);
26         // 必須設置圖片大小,否則不顯示圖片
27         drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
28         Button btn_left = (Button) findViewById(R.id.btn_left);
29         Button btn_top = (Button) findViewById(R.id.btn_top);
30         Button btn_right = (Button) findViewById(R.id.btn_right);
31         Button btn_bottom = (Button) findViewById(R.id.btn_bottom);
32         btn_left.setOnClickListener(this);
33         btn_top.setOnClickListener(this);
34         btn_right.setOnClickListener(this);
35         btn_bottom.setOnClickListener(this);
36     }
37 
38     @Override
39     public void onClick(View v) {
40         if (v.getId() == R.id.btn_left) {
41             btn_icon.setCompoundDrawables(drawable, null, null, null);
42         } else if (v.getId() == R.id.btn_top) {
43             btn_icon.setCompoundDrawables(null, drawable, null, null);
44         } else if (v.getId() == R.id.btn_right) {
45             btn_icon.setCompoundDrawables(null, null, drawable, null);
46         } else if (v.getId() == R.id.btn_bottom) {
47             btn_icon.setCompoundDrawables(null, null, null, drawable);
48         }
49     }
50 
51     public static void startHome(Context mContext) {
52         Intent intent = new Intent(mContext, class__2_3_4.class);
53         mContext.startActivity(intent);
54     }
55 }

Android 開發筆記___圖像按鈕__imageButton