第二种方法也要新建一个图片+文字的xml布局文件,然后写一个类继承自LinearLayout。在主程序里实例化并设置相应参数。这种方式也是我最推荐的一种。
第一部分:myimgbtn_layout.xml
<?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 文件:
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:
<?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>
第四部分,主程序:
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,
"您好",
Toast.LENGTH_SHORT)
.show();
}
});
}
}
这种方法很直观简单,与之第一种用Gallery方法而言更容易理解。就是自定义一个类,第一种方法虽然不用自定义类,但是Gallery相关的适配器配置和那个View相关的如果第一次会不大习惯。这种效果也不错,图就不贴了。尤其适合做那种背景是纯色,里面嵌套图片+文字。就是360手机安全卫士的主窗口,大家可以看下。应该就是为这种方式做的。美中不足的是,360手机安全卫士的主窗口里,你点击一下,背景会变。也就是说这还缺少个onTouchListener,稍后我补上。