Android 開發筆記___映像按鈕__imageButton

來源:互聯網
上載者:User

標籤:package   port   ide   pre   cli   strong   派生   btn   draw   

IMAGEBUTTON

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

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

 

  • 在IME無法輸入的字元和特殊字型顯示的字串,就適合用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     <LinearLayout20         android:layout_width="match_parent"21         android:layout_height="wrap_content"22         android:layout_marginTop="10dp"23         android:orientation="horizontal">24 25         <Button26             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         <Button35             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         <Button44             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         <Button53             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     @Override21     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     @Override39     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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.