Android實現自定義帶文字和圖片的Button

在Android開發中經常會需要用到帶文字和圖片的button,下面來講解一下常用的實現辦法。

一.用系統自帶的Button實現

  最簡單的一種辦法就是利用系統自帶的Button來實現,這種方式代碼量最小。在Button的屬性中有一個是drawableLeft,這個 屬性可以把圖片設置在文字的左邊,但是這種方式必須讓icon的背景色是透明的,如果icon的背景色不是透明的話,會導致點擊按鈕時icon部分的背景 色不會發生變化。

主要代碼:

<Button android:id="@+id/bt3"android:layout_marginTop="4dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="火車" android:textSize="16sp" android:textColor="#000000" android:paddingLeft="5dp" android:paddingTop="5dp" android:paddingRight="5dp" android:paddingBottom="5dp" android:drawableLeft="@drawable/line_bus_icon" android:background="@drawable/button_bg"> </Button>

  實現效果:

  

  如果要讓文字在圖標下方,改成drawableTop即可。?

二.繼承系統的Button然后進行重繪

package com.test;import android.content.Context;
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.util.AttributeSet; import android.widget.Button; public class ImageTextButton2 extends Button { private int resourceId = 0; private Bitmap bitmap; public ImageTextButton2(Context context) { super(context,null); } public ImageTextButton2(Context context,AttributeSet attributeSet) { super(context, attributeSet); this.setClickable(true); resourceId = R.drawable.icon; bitmap = BitmapFactory.decodeResource(getResources(), resourceId); } public void setIcon(int resourceId) { this.bitmap = BitmapFactory.decodeResource(getResources(), resourceId); invalidate(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub // 圖片頂部居中顯示 int x = (this.getMeasuredWidth() - bitmap.getWidth())/2; int y = 0; canvas.drawBitmap(bitmap, x, y, null); // 坐標需要轉換,因為默認情況下Button中的文字居中顯示 // 這里需要讓文字在底部顯示 canvas.translate(0,(this.getMeasuredHeight()/2) - (int) this.getTextSize()); super.onDraw(canvas); } }

  然后再布局文件中調用:

<com.test.ImageTextButton2android:id="@+id/bt2"android:layout_marginTop="10dp" android:text="hello" android:textSize="15dp" android:textColor="#000000" android:layout_width="60dp" android:layout_height="70dp" android:background="@drawable/button_bg" />

  注意,在xml文件中調用時,對于layout_width和layout_height兩個屬性千萬不能用wrap_content,否則會導致按鈕顯示出來的只有文字部分。

三.繼承布局文件

  分析發現一個帶文字和icon的button其實可以看成一個線性布局或相對布局,因此可以繼承布局來實現。

  先實現一個button的布局文件img_text_bt.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/imgview" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/icon"> </ImageView> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_below="@id/imgview"> </TextView> </RelativeLayout>

  然后去繼承RelativeLayout布局:

package com.test;import android.content.Context;
import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; public class ImageTextButton1 extends RelativeLayout { private ImageView imgView; private TextView textView; public ImageTextButton1(Context context) { super(context,null); } public ImageTextButton1(Context context,AttributeSet attributeSet) { super(context, attributeSet); LayoutInflater.from(context).inflate(R.layout.img_text_bt, this,true); this.imgView = (ImageView)findViewById(R.id.imgview); this.textView = (TextView)findViewById(R.id.textview); this.setClickable(true); this.setFocusable(true); } public void setImgResource(int resourceID) { this.imgView.setImageResource(resourceID); } public void setText(String text) { this.textView.setText(text); } public void setTextColor(int color) { this.textView.setTextColor(color); } public void setTextSize(float size) { this.textView.setTextSize(size); } }

  然后就可以在需要的xml文件中調用:

<com.test.ImageTextButton1 android:id="@+id/bt1"android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_bg" />

  再在Activity中使用:

     bt1 = (ImageTextButton1)findViewById(R.id.bt1);bt1.setText("icon");bt1.setTextColor(Color.rgb(0, 0, 0));bt1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "bt1被點擊了", Toast.LENGTH_SHORT).show(); } });

  三種不同方法最后的運行效果:

  工程源碼下載地址:http://files.cnblogs.com/dolphin0520/TestImgTextButton.rar

轉載于:https://www.cnblogs.com/Free-Thinker/p/5571875.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/372850.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/372850.shtml
英文地址,請注明出處:http://en.pswp.cn/news/372850.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql語句中的注釋方法_MySQL語句注釋方式簡介

MySQL支持三種注釋方式&#xff1a;1.從‘#字符從行尾。2.從‘-- 序列到行尾。請注意‘-- (雙破折號)注釋風格要求第2個破折號后面至少跟一個空格符(例如空格、tab、換行符等等)。3.從/*序列到后面的*/序列。結束序列不一定在同一行中&#xff0c;因此該語法允許注釋跨越多行。…

aqlserver實用程序_sqlserver命令提示實用工具的介紹

除上述的圖形化管理工具外&#xff0c;SQL Server2008還提供了大量的命令行實用工具&#xff0c;包括bcp、dtexec、dtutil、osql、reconfig、sqlcmd、sqlwb和tablediff等&#xff0c;下面進行簡要說明。dtexec實用工具用于配置和執行SQL Server2008 Intgration Services包。用戶…

使用Java和Scala將Play Framework 2應用程序部署到Openshift

幾個星期&#xff0c; 馬克阿特伍德 &#xff08; Mark Atwood&#xff09; &#xff0c; 豪爾赫阿里斯 &#xff08; Jorge Aliss &#xff09;和我塞巴斯蒂安 斯卡塔諾 &#xff08; SebastinScarano&#xff09;參加了紅帽網絡研討會LETS PLAY&#xff01; 在云端&#xff1…

LintCode 387: Smallest Difference

LintCode 387: Smallest Difference 題目描述 給定兩個整數數組&#xff08;第一個是數組A&#xff0c;第二個是數組B&#xff09;&#xff0c;在數組A中取A[i]&#xff0c;數組B中取B[j]&#xff0c;A[i]和B[j]兩者的差越小越好(|A[i] - B[j]|)。返回最小差。 樣例 給定數組A …

android框架----下沉文字Titanic的使用

Titanic is a simple illusion obtained by applying an animated translation on the TextView TextPaint Shaders matrix. Titanic的使用 Titanic的使用&#xff0c;項目結構如下&#xff1a; 一、下載Titanic并且部署到項目中 Titanic的項目地址&#xff1a; https://github…

linux 自動安裝mysql_Linux安裝mysql

一、下載這里我創建了一目錄software用于存放我們待會要下載的mysql包&#xff0c;先去到該目錄命令&#xff1a;cd /software命令&#xff1a;wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar下載完成后&#xff0c;你會在software這個…

Quartz Scheduler插件–隱藏的寶藏

盡管在官方文檔中進行了簡要描述&#xff0c;但我相信Quartz插件了解得還不夠多&#xff0c;看看它們有多有用。 本質上&#xff0c;Quartz中的插件是方便的類&#xff0c;用于包裝基礎偵聽器的注冊。 您可以自由編寫自己的插件&#xff0c;但我們將專注于Quartz隨附的現有插件…

mysql查詢表名匹配只有字母的_MySQL按某些匹配字母查詢表

MySQL查詢是MySQL的核心功能&#xff0c;有時候我們需要查找帶有某些匹配字母的表。下文對該MySQL查詢方式作了詳細的介紹&#xff0c;供您參考。在MySQL中我們可以使用LIKE或者NOT LIKE操作符進行比較。在MySQL中模式默認是不區分大小寫的。查詢示例&#xff0c;student表----…

hdu 1181(Floyed)

變形課 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 20748 Accepted Submission(s): 7494 Problem Description呃......變形課上Harry碰到了一點小麻煩,因為他并不像Hermione那樣能夠記住所有的咒語而隨意的…

讀書筆記-你不知道的JS上-混入與原型

繼承 mixin混合繼承 function mixin(obj1, obj2) {for (var key in obj2) {//重復不復制if (!(key in obj1)) {obj1[key] obj2[key];}}return obj1;} 這種復制是淺復制&#xff0c;對象或者數組函數等都是同一個引用&#xff0c;改變obj1的會同時影響obj2。 寄生繼承 ... 隱式…

JUnit和Hamcrest:在assertEquals上進行改進

在我的博客文章中&#xff0c;Java越來越接受靜態導入嗎&#xff1f; &#xff0c;我討論了在Java中越來越多地使用靜態導入來使代碼在某些情況下更流暢。 Java 單元測試特別受靜態導入的影響&#xff0c;在此博客文章中&#xff0c;我提供了一個簡單的示例&#xff0c;說明如何…

mysql delete temporary denied_這些錯誤是什么意思?djang中的mysql

我試著運行一個程序&#xff0c;我被給予了一個例子&#xff0c;它就像一個購物網站&#xff0c;使用MySQL數據庫而不是Django提供的原始數據庫&#xff01;我只是想看看有沒有人理解這些錯誤的含義&#xff1f;任何信息都將不勝感激&#xff01;我本可以提供網頁的代碼&#x…

C語言 · 芯片測試

基礎練習 芯片測試 時間限制&#xff1a;1.0s 內存限制&#xff1a;512.0MB問題描述有n&#xff08;2≤n≤20&#xff09;塊芯片&#xff0c;有好有壞&#xff0c;已知好芯片比壞芯片多。每個芯片都能用來測試其他芯片。用好芯片測試其他芯片時&#xff0c;能正確給出被測試…

Animation用法

測試代碼及說明&#xff1a; <!DOCTYPE html> <html lang"en-US"> <head><meta charset"UTF-8"><title>Simple CSS3 Animation</title><style type"text/css">#demo {position: absolute;left: 30%;t…

mysql dese_MySQL 5.6-類似于DENSE_RANK的功能,無需訂購

小編典典對于 MySQL版本<8.0(OP的版本是5.6)&#xff1a;問題陳述看起來需要DENSE_RANK功能groupVarian; 但是事實并非如此。正如 GordonLinoff解釋的那樣 &#xff1a;您似乎希望按它們在數據中出現的順序來枚舉它們。假設您的表名是t(請為您的代碼相應地更改表名和字段名)…

Spring和JSF集成:動態導航

通常&#xff0c;您的JSF應用程序將需要超越基本的靜態導航并開始做出動態導航決策。 例如&#xff0c;您可能想根據用戶的年齡重定向他們。 大多數JSF教程建議通過將命令的action屬性綁定到支持bean來實現動態導航&#xff1a; <h:commandButton action"#{bean.action…

通過富文本改變UITextFieldPlaceholder顏色

1、通過屬性 a、 //文字屬性(一般) NSMutableDictionary *attrs [NSMutableDictionary dictionary]; attrs[NSForegroundColorAttributeName] [UIColor blueColor]; NSAttributedString *placeholderStr [[NSAttributedString alloc] initWithString:"手機號" a…

阻塞/非阻塞/同步/異步方法和多線程的關系?沒有任何關系,倆不挨著

1.阻塞非阻塞異步同步是針對方法說的&#xff0c;是評判一個方法運行狀態的。和多線程完全兩個級別。 2.阻塞非阻塞異步同步是針對方法說的&#xff0c;是評判一個方法運行狀態的。和多線程完全兩個級別。 3.阻塞非阻塞異步同步是針對方法說的&#xff0c;是評判一個方法運行狀…

mysql備份 where_MySQL備份與還原

1.mysqldumpmysqlbinlog介紹mysqldump備份結合binlog日志恢復。MySQL備份一般采取全庫備份加日志備份的方式&#xff0c;例如每天執行一次全備份&#xff0c;每小時執行一次二進制日志備份&#xff0c;這樣在MySQL故障后可以使用全備份和日志備份將數據恢復到最后一個二進制日志…

JMeter:負載測試關系數據庫

Apache JMeter是完全使用Java編寫的性能測試工具。 可以在請求/響應模型上運行的任何應用程序都可以使用JMeter進行負載測試。 關系數據庫也不例外&#xff1a;接收sql查詢&#xff0c;執行它們并返回執行結果。 我將向您展示使用JMeter的圖形用戶界面設置測試方案有多么容易。…