android中的TabActivity是专门用来放置TabHost组件的,但是很遗憾,非常固定,必须放在顶部,对于单手使用手机的人来说,很不方便。。。
所以研究了一下如何将TabBar放置底部
具体思路是在xml布局文件中,将TabWidget放于FrameLayout的下面(两者是垂直线性的关系。。),并分配各个组件的权重
另外,如果想去掉Tab中那个丑陋的白线,可以通过设置其Padding实现,不敢多写了,贴代码吧,很简单,重要的是思路。。。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#ff000000"
/>
</LinearLayout>
</TabHost>
public class indexActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
TabHost tabHost= (TabHost)findViewById(android.R.id.tabhost);
// TabWidget tabWidget = tabHost.getTabWidget();
// LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentView(), true);
TabSpec spec;
Intent intent;
intent = new Intent(this,testActivity.class);//新建一个Intent用作Tab1显示的内容(这里ShowActivity自定义)
spec = tabHost.newTabSpec("tab1")//新建一个 Tab
.setIndicator("Tab1")//设置名称以及图标
.setContent(intent);//设置显示的intentx
tabHost.addTab(spec);//添加进去
intent = new Intent(this,testActivity.class);//新建一个Intent用作Tab1显示的内容(这里ShowActivity自定义)
spec = tabHost.newTabSpec("tab2")//新建一个 Tab
.setIndicator("Tab2")//设置名称以及图标
.setContent(intent);//设置显示的intentx
tabHost.addTab(spec);//添加进去
intent = new Intent(this,testActivity.class);//新建一个Intent用作Tab1显示的内容(这里ShowActivity自定义)
spec = tabHost.newTabSpec("tab3")//新建一个 Tab
.setIndicator("Tab3")//设置名称以及图标
.setContent(intent);//设置显示的intentx
tabHost.addTab(spec);//添加进去
spec = tabHost.newTabSpec("").setContent(intent).setIndicator("第三个");
tabHost.addTab(spec);
tabHost.setCurrentTab(0);//设置默认选中项
}
}