很多时候我们需要复用一些控件,比如导航菜单等等,以下是代码
第一步,新建一个xml文件,放在layout下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_gradient"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
>
<TextView
android:id="@+id/Titletext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="21sp"
android:layout_centerVertical="true"
android:textColor="#ffffffff"
android:textStyle="bold"
android:text="@string/custom_list_title"
/>
<!-- 后退 -->
<ImageButton
android:id="@+id/TitleBackBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
<!-- 发布信息 -->
<ImageButton
android:id="@+id/imageButtonFaYan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:layout_toLeftOf="@+id/TitleBackBtn"
android:background="@drawable/fayan"
/>
<!-- 我的设置 -->
<ImageButton
android:id="@+id/imageButtonSheZhi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:layout_toLeftOf="@+id/imageButtonFaYan"
android:background="@drawable/shezhi"
/>
<!-- 首页 -->
<ImageButton
android:id="@+id/imageButtonHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="12dp"
android:layout_toLeftOf="@+id/imageButtonSheZhi"
android:background="@drawable/home"
/>
</RelativeLayout>
第二步,写对应自定义控件的代码
package com.wj.custom.ui;
import com.example.wjloginandlisttest.R;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class TitleUi extends RelativeLayout {
private Context mContext; // 上下文
private Activity mActivity; // 活动对象
private ImageButton ImgbtnBack;
private TextView titleText;
public TitleUi(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public TitleUi(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.title_ui, this);
initBtn();
}
private void initBtn() {
// 获得上下文
mContext = getContext();
// 这里将Context转换为Activity.
mActivity = (Activity) mContext;
titleText = (TextView) findViewById(R.id.Titletext);
ImgbtnBack = (ImageButton) findViewById(R.id.TitleBackBtn);
ImgbtnBack.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
// KeyEvent.KEYCODE_BACK);
//onKeyDown(KeyEvent.KEYCODE_BACK, newEvent);
//Log.v("aa", "我点击了按钮");
mActivity.finish();//返回
}
});
}
/**
* 设置显示的标题文字
*/
public void setTitleText(String text) {
titleText.setText(text);
}
}
第三步,在实际窗体中使用
<?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" >
<com.wj.custom.ui.TitleUi
android:id="@+id/topTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂无内容!" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center|center_horizontal|center_vertical" >
<Button
android:id="@+id/NextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一页"
android:background="@drawable/text_view_border"
/>
</LinearLayout>
</LinearLayout>
第四步,对于控件的特殊属性赋值
package com.example.wjloginandlisttest;
import java.util.ArrayList;
import java.util.HashMap;
import com.wj.custom.ui.TitleUi;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class CustomList extends ListActivity {
private TitleUi topTitle;//自定义的标题栏
private ArrayList<HashMap<String, Object>> listItems; //存放文字、图片信息
private SimpleAdapter listItemAdapter; //适配器
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
super.onCreate(icicle);
setContentView(R.layout.activity_custom_list);
initListView();
this.setListAdapter(listItemAdapter);
init();//初始化数据
}
private void init(){
topTitle=(TitleUi) this.findViewById(R.id.topTitle);
topTitle.setTitleText("正能量交流区");
}
/**
* 设置适配器内容
*/
private void initListView() {
listItems = new ArrayList<HashMap<String, Object>>();
for(int i=0;i<10;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemTitle", "Musich第三方范围范围额范围无非威风威风威风我额范围范围威风威风哥哥v: "+i); //文字
map.put("ItemImage", R.drawable.i1);//图片
listItems.add(map);
}
//生成适配器的Item和动态数组对应的元素
listItemAdapter = new SimpleAdapter(this,listItems,//数据源
R.layout.list_item,//ListItem的XML布局实现
//动态数组与ImageItem对应的子项
new String[] {"ItemTitle", "ItemImage"},
//ImageItem的XML文件里面的一个ImageView,两个TextView ID
new int[] {R.id.ItemTitle, R.id.ItemImage}
);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
Log.e("position", "" + position);
//setTitle("你点击第"+position+"行");
Toast.makeText(this, "你单击了"+position, Toast.LENGTH_SHORT).show();
startActivity(new Intent(CustomList.this, BbsContent.class));
}
}