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

來源:互聯網
上載者:User

第二種方法也要建立一個圖片+文字的xml布局檔案,然後寫一個類繼承自LinearLayout。在主程式裡執行個體化並設定相應參數。這種方式也是我最推薦的一種。第一部分:myimgbtn_layout.xml[html] <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:alpha="20"      android:background="#87CE"      android:orientation="vertical"       >        <ImageView          android:id="@+id/img"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="center_horizontal"          android:paddingBottom="5dip"          android:paddingTop="5dip" />      <TextView           android:id="@+id/text"          android:layout_width="wrap_content"          android:layout_height="wrap_content"             android:textColor="#FF6100"             android:textSize="30dip"            android:layout_gravity="center_vertical"/>        </LinearLayout>   第二部分,與之布局相對應的MyImgBtn.java 檔案:[java] package yan.guoqi.testimgbtn;    import android.content.Context;  import android.graphics.Color;  import android.util.AttributeSet;  import android.view.LayoutInflater;  import android.view.MotionEvent;  import android.view.View;  import android.view.View.OnTouchListener;  import android.widget.ImageView;  import android.widget.LinearLayout;  import android.widget.TextView;    public class MyImgBtn extends LinearLayout {        private ImageView mImgView = null;      private TextView mTextView = null;      private Context mContext;              public MyImgBtn(Context context, AttributeSet attrs) {          super(context, attrs);          // TODO Auto-generated constructor stub          LayoutInflater.from(context).inflate(R.layout.myimgbtn_layout, this, true);          mContext = context;          mImgView = (ImageView)findViewById(R.id.img);          mTextView = (TextView)findViewById(R.id.text);                                  }           /*設定圖片介面*/      public void setImageResource(int resId){          mImgView.setImageResource(resId);      }            /*設定文字介面*/      public void setText(String str){          mTextView.setText(str);      }      /*設定文字大小*/      public void setTextSize(float size){          mTextView.setTextSize(size);      }          //     /*設定觸摸介面*/  //    public void setOnTouch(OnTouchListener listen){  //        mImgView.setOnTouchListener(listen);  //        //mTextView.setOnTouchListener(listen);  //    }    }   第三部分,主布局main.xml:[html] <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical"       android:background="@drawable/main_background2">        <TextView          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="@string/hello" />      <yan.guoqi.testimgbtn.MyImgBtn          android:id="@+id/MyIBtn_1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="center_horizontal"                  android:clickable="true"          android:focusable="true"                />    </LinearLayout>  第四部分,主程式:[java] package yan.guoqi.testimgbtn;    import android.app.Activity;  import android.os.Bundle;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Toast;    public class TestImgBtnActivity extends Activity {           private MyImgBtn MyIBtn1 = null;            /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);                    MyIBtn1 = (MyImgBtn)findViewById(R.id.MyIBtn_1);          MyIBtn1.setImageResource(R.drawable.ic_launcher);          MyIBtn1.setText("歡迎光臨");          MyIBtn1.setTextSize(24.0f);          //MyIBtn1.setOnTouch(new MyOnTouchListener());          MyIBtn1.setOnClickListener(new OnClickListener() {                            public void onClick(View arg0) {                  // TODO Auto-generated method stub                  Toast.makeText(TestImgBtnActivity.this,                                 "您好",  www.2cto.com                               Toast.LENGTH_SHORT)                                 .show();                                }          });      }  }   這種方法很直觀簡單,與之第一種用Gallery方法而言更容易理解。就是自訂一個類,第一種方法雖然不用自訂類,但是Gallery相關的適配器配置和那個View相關的如果第一次會不大習慣。這種效果也不錯,圖就不貼了。尤其適合做那種背景是純色,裡面嵌套圖片+文字。就是360手機安全衛士的主視窗,大家可以看下。應該就是為這種方式做的。美中不足的是,360手機安全衛士的主視窗裡,你點擊一下,背景會變。也就是說這還缺少個onTouchListener,稍後我補上。

聯繫我們

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