Android自訂“圖片+文字”控制項四種實現方法之 二--------個人最推薦的一種

來源:互聯網
上載者:User

標籤:

http://blog.csdn.net/yanzi1225627/article/details/8633872

第二種方法也要建立一個圖片+文字的xml布局檔案,然後寫一個類繼承自LinearLayout。在主程式裡執行個體化並設定相應參數。這種方式也是我最推薦的一種。

第一部分:myimgbtn_layout.xml

[html] view plaincopyprint? 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:alpha="20"  
  6.     android:background="#87CE"  
  7.     android:orientation="vertical"   
  8.     >  
  9.   
  10.     <ImageView  
  11.         android:id="@+id/img"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_gravity="center_horizontal"  
  15.         android:paddingBottom="5dip"  
  16.         android:paddingTop="5dip" />  
  17.     <TextView   
  18.         android:id="@+id/text"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"     
  21.         android:textColor="#FF6100"     
  22.         android:textSize="30dip"    
  23.         android:layout_gravity="center_vertical"/>  
  24.   
  25.   
  26.   
  27. </LinearLayout>  


第二部分,與之布局相對應的MyImgBtn.java 檔案:

[java] view plaincopyprint? 
  1. package yan.guoqi.testimgbtn;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Color;  
  5. import android.util.AttributeSet;  
  6. import android.view.LayoutInflater;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.View.OnTouchListener;  
  10. import android.widget.ImageView;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.TextView;  
  13.   
  14. public class MyImgBtn extends LinearLayout {  
  15.   
  16.     private ImageView mImgView = null;  
  17.     private TextView mTextView = null;  
  18.     private Context mContext;  
  19.   
  20.       
  21.     public MyImgBtn(Context context, AttributeSet attrs) {  
  22.         super(context, attrs);  
  23.         // TODO Auto-generated constructor stub  
  24.         LayoutInflater.from(context).inflate(R.layout.myimgbtn_layout, this, true);  
  25.         mContext = context;  
  26.         mImgView = (ImageView)findViewById(R.id.img);  
  27.         mTextView = (TextView)findViewById(R.id.text);  
  28.                   
  29.           
  30.     }  
  31.   
  32.   
  33.   
  34.    /*設定圖片介面*/  
  35.     public void setImageResource(int resId){  
  36.         mImgView.setImageResource(resId);  
  37.     }  
  38.       
  39.     /*設定文字介面*/  
  40.     public void setText(String str){  
  41.         mTextView.setText(str);  
  42.     }  
  43.     /*設定文字大小*/  
  44.     public void setTextSize(float size){  
  45.         mTextView.setTextSize(size);  
  46.     }  
  47.   
  48.   
  49.   
  50.   
  51. //     /*設定觸摸介面*/  
  52. //    public void setOnTouch(OnTouchListener listen){  
  53. //        mImgView.setOnTouchListener(listen);  
  54. //        //mTextView.setOnTouchListener(listen);  
  55. //    }  
  56.   
  57. }  


第三部分,主布局main.xml:

[html] view plaincopyprint? 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"   
  6.     android:background="@drawable/main_background2">  
  7.   
  8.     <TextView  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/hello" />  
  12.     <yan.guoqi.testimgbtn.MyImgBtn  
  13.         android:id="@+id/MyIBtn_1"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_gravity="center_horizontal"          
  17.         android:clickable="true"  
  18.         android:focusable="true"        
  19.         />  
  20.   
  21. </LinearLayout>  

第四部分,主程式:

[java] view plaincopyprint? 
  1. package yan.guoqi.testimgbtn;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Toast;  
  8.   
  9. public class TestImgBtnActivity extends Activity {  
  10.      
  11.     private MyImgBtn MyIBtn1 = null;  
  12.       
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         MyIBtn1 = (MyImgBtn)findViewById(R.id.MyIBtn_1);  
  20.         MyIBtn1.setImageResource(R.drawable.ic_launcher);  
  21.         MyIBtn1.setText("歡迎光臨");  
  22.         MyIBtn1.setTextSize(24.0f);  
  23.         //MyIBtn1.setOnTouch(new MyOnTouchListener());  
  24.         MyIBtn1.setOnClickListener(new OnClickListener() {  
  25.               
  26.             public void onClick(View arg0) {  
  27.                 // TODO Auto-generated method stub  
  28.                 Toast.makeText(TestImgBtnActivity.this,  
  29.                                "您好",  
  30.                                Toast.LENGTH_SHORT)  
  31.                                .show();  
  32.                   
  33.             }  
  34.         });  
  35.     }  
  36. }  


這種方法很直觀簡單,與之第一種用Gallery方法而言更容易理解。就是自訂一個類,第一種方法雖然不用自訂類,但是Gallery相關的適配器配置和那個View相關的如果第一次會不大習慣。這種效果也不錯,圖就不貼了。尤其適合做那種背景是純色,裡面嵌套圖片+文字。就是360手機安全衛士的主視窗,大家可以看下。應該就是為這種方式做的。美中不足的是,360手機安全衛士的主視窗裡,你點擊一下,背景會變。也就是說這還缺少個onTouchListener,稍後我補上。

Android自訂“圖片+文字”控制項四種實現方法之 二--------個人最推薦的一種

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.