标签:private public 开发 tabs frame
主要代码
public class TabsFrame extends FragmentActivity {
private FragmentTabHost fragmentTabHost;
private String texts[] = { "首页", "消息", "好友", "广场" };
private int imageButton[] = { R.drawable.selector1,
R.drawable.selector2,
R.drawable.selector3,
R.drawable.selector4};
private Class fragmentArray[] = {Page1.class,
Page2.class,
Page3.class,
Page4.class};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabsframe);
// 实例化tabhost
fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(),R.id.maincontent);
for (int i = 0; i < texts.length; i++) {
TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
fragmentTabHost.addTab(spec, fragmentArray[i], null);
//设置背景(必须在addTab之后,由于需要子节点(底部菜单按钮)否则会出现空指针异常)
fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bgselector);
}
}
private View getView(int i) {
//取得布局实例
View view=View.inflate(TabsFrame.this, R.layout.tabcontent, null);
//取得布局对象
ImageView imageView=(ImageView) view.findViewById(R.id.image);
TextView textView=(TextView) view.findViewById(R.id.text);
//设置图标
imageView.setImageResource(imageButton[i]);
//设置标题
textView.setText(texts[i]);
return view;
}
public boolean onKeyDown(int KeyCode,KeyEvent Event){
if(KeyCode==KeyEvent.KEYCODE_BACK){
Intent intent=new Intent(TabsFrame.this,MainActivity.class);
startActivity(intent);
finish();
}
return true;
}
}
Layout文件
tabsframe
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 存放主要页面内容 -->
<FrameLayout
android:id="@+id/maincontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<!-- 底层菜单 -->
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/tab_bg" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" >
</FrameLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
tabcontent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView
android:id="@+id/text"
android:padding="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
/>
</LinearLayout>
这样就可以实现底部菜单栏啦!
本文出自 “软件学习总结” 博客,请务必保留此出处http://bigcrab.blog.51cto.com/10626858/1689147
标签:private public 开发 tabs frame
原文地址:http://bigcrab.blog.51cto.com/10626858/1689147