http://blog.csdn.net/yanzi1225627/article/details/8633872
The second method is to create a new image + text XML layout file, and then write a class that inherits from LinearLayout. Instantiate and set the corresponding parameters in the main program. This way is also my most recommended one.
The first part: Myimgbtn_layout.xml
[HTML]View Plaincopyprint?
- <? 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="
- 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>
The second part, corresponding to the layout of the Myimgbtn.java file:
[Java]View Plaincopyprint?
- 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);
- }
- / * Set Picture interface * /
- public void Setimageresource (int resId) {
- Mimgview.setimageresource (RESID);
- }
- / * Set the Text interface * /
- public void SetText (String str) {
- Mtextview.settext (str);
- }
- / * Set the text size * /
- public void settextsize (float size) {
- Mtextview.settextsize (size);
- }
- /* Set Touch interface */
- public void Setontouch (Ontouchlistener listen) {
- Mimgview.setontouchlistener (listen);
- Mtextview.setontouchlistener (listen);
- // }
- }
The third part, the main layout main.xml:
[HTML]View Plaincopyprint?
- <? 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>
Part IV, main program:
[Java]View Plaincopyprint?
- 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 ("Welcome");
- 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,
- "Hello",
- Toast.length_short)
- . Show ();
- }
- });
- }
- }
This method is straightforward and easy to understand with the first method of gallery. is to customize a class, although the first method does not use a custom class, but the gallery related adapter configuration and that view related if the first time will not be very accustomed. This effect is also good, the picture is not affixed. Especially suitable for doing that background is solid color, inside nested picture + text. IS 360 mobile phone security defender's main window, you can look down. It should be done in this way. In the ointment is, 360 mobile phone Security defender's main window, you click, the background will change. That means there's still a ontouchlistener missing, and I'll make it up later.
Android Custom "picture + text" Control of the four methods of implementation of the two--------personal most recommended one