【实例简介】
简单的抽屉效果和底部导航栏的效果。
【实例截图】
【核心代码】
package com.example.ct;
import java.util.ArrayList;
import java.util.List;
import com.example.qq.MainActivity;
import com.example.qq.R;
import dibudaohanglan.AMainActivity;
import dibudaohanglan.BMainActivity;
import dibudaohanglan.CMainActivity;
import dibudaohanglan.DMainActivity;
import dibudaohanglan.Dbdhl_NO;
import adapter.Adapter1;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
/**
* 抽屉效果的实现,底部导航栏的实现
* @author Administrator
*
*/
@SuppressLint("NewApi")
public class CoutiActivity extends TabActivity implements
OnCheckedChangeListener
{
private DrawerLayout mDrawerLayout = null;
private List list;
private ContentAdapter adapter;
private Button kit;
public TabHost mTabHost;
private Intent mAIntent;
private Intent mBIntent;
private Intent mCIntent;
private Intent mDIntent;
private Intent mEIntent;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
mTabHost.setCurrentTabByTag("A_TAB");
}
};
// 广播接收器
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
mTabHost.setCurrentTabByTag("A_TAB");
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//标题消失
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
/*//鍘婚敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�
ActionBar actionBar=getActionBar();
actionBar.hide();*/
this.mAIntent = new Intent(this, AMainActivity.class);
this.mBIntent = new Intent(this, BMainActivity.class);
this.mCIntent = new Intent(this, CMainActivity.class);
this.mDIntent = new Intent(this, DMainActivity.class);
((RadioButton) findViewById(R.id.radio_button0))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_button1))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_button2))
.setOnCheckedChangeListener(this);
((RadioButton) findViewById(R.id.radio_button3))
.setOnCheckedChangeListener(this);
setupIntent();
//左边的listview
ListView listView=(ListView) findViewById(R.id.left_drawer);
initData();
adapter=new ContentAdapter(this, list);
listView.setAdapter(adapter);
//侧滑的listview的条目点击事件
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),list.get(arg2).getText() , 2000).show();
}
});
//zhaodao找到侧滑的空间
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//找到图片控件
ImageView iv=(ImageView) this.findViewById(R.id.iv);
//瀹革缚绗傜憴鎺戞禈閻楋拷
iv.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 閿熸枻鎷烽挳閿熸枻鎷烽敓閾帮綇鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿燂拷
//d打开左边的侧滑栏,显示出listview
mDrawerLayout.openDrawer(Gravity.LEFT);
Toast.makeText(getApplicationContext(), "图片的点击事件", 2000).show();
}
});
}
//鐠佸墽鐤嗛崶鍓уlistview
/**
* 向侧滑的listview里边添加数据,测试,简单的添加了两条
*/
private void initData() {
list=new ArrayList();
list.add(new ContentModel(R.drawable.link, "双刀"));
list.add(new ContentModel(R.drawable.qq, "qq"));
}
/* public void db11(View v){
startActivity(new Intent(CoutiActivity.this,Dbdhl.class));
}*/
//设置点击按钮时候的跳转
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
switch (buttonView.getId()) {
case R.id.radio_button0:
this.mTabHost.setCurrentTabByTag("A_TAB");
break;
// 设置
case R.id.radio_button1:
this.mTabHost.setCurrentTabByTag("B_TAB");
break;
// 保存
case R.id.radio_button2:
// 请输入用户名
this.mTabHost.setCurrentTabByTag("C_TAB");
break;
case R.id.radio_button3:
this.mTabHost.setCurrentTabByTag("D_TAB");
break;
}
}
}
/**
* 底部导航栏的加载图片,文字,activity
*/
private void setupIntent() {
this.mTabHost = getTabHost();
TabHost localTabHost = this.mTabHost;
localTabHost.addTab(buildTabSpec("A_TAB", R.string.title_activity_amain,
R.drawable.weather, this.mAIntent));
localTabHost.addTab(buildTabSpec("B_TAB", R.string.title_activity_bmain,
R.drawable.icon_2_n, this.mBIntent));
localTabHost.addTab(buildTabSpec("C_TAB", R.string.title_activity_cmain,
R.drawable.icon_3_n, this.mCIntent));
localTabHost.addTab(buildTabSpec("D_TAB", R.string.title_activity_dmain,
R.drawable.icon_4_n, this.mDIntent));
}
/**
* TabHost.TabSpec 一个用在底部导航栏的方法
* @param tag
* @param resLabel
* @param resIcon
* @param content
* @return
*/
private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
final Intent content) {
return this.mTabHost
.newTabSpec(tag)
.setIndicator(getString(resLabel),
getResources().getDrawable(resIcon))
.setContent(content);
}
}