布局页面
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/frame_home">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:scrollbars="none">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/frame_pic">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
</FrameLayout>
<TabWidget
android:layout_weight="1"
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="40dip"
android:orientation="horizontal"
android:background="#BBBBBB">
</TabWidget>
</LinearLayout>
</TabHost>
其实这就是一个选项卡,按我们一般的思维,就是要有一组按钮和对应的界面。这里对应过来就是tabwidget和FrameLayout。
这里布局的时候要注意两个地方,一个是必须要有FrameLayout,而且它的ID必须是 @android:id/tabcontent 这个应该是系统内置的,方便我们操作。
二是要有TabWidget,(翻译过来就是tab装饰)就是tab所在的位置了(看那个提示的小图标就知道了),同样它的ID也是系统规定好的,android:id="@android:id/tabs"。
(不过还是没找到,明天来了再看看)解决了,写在最下面了。
mainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View homeButton = LayoutInflater.from(this).inflate(R.layout.home_icon,null);//这里为了让点了之后有效果,用layout来做的。类似于超链接。
View picButton = LayoutInflater.from(this).inflate(R.layout.picture_icon,null);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); //得到tabhost
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("Home").setIndicator(homeView).setContent(R.id.frame_home)); //把按钮和界面对应起来 frame_home是从上面的Frame里面来的
tabHost.addTab(tabHost.newTabSpec("Picture").setIndicator(picView).setContent(R.id.frame_pic));
}
home “Button” 的样式
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/select_tab_home"/>
</RelativeLayout>
select_tab_home的“超链接样式(这些样式一般放在drawable里)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/icon_home_sel"></item>
<item android:state_selected="false" android:drawable="@drawable/icon_home_nor"></item>
</selector>
------------------------------------------------------------------------------------------第二天-------------------------------------------------------------------------------------------------------------------
tabwidget一直显示在最下面
首先来说一下layout_weight. weight也就是某个控件的权值。 控件的权值的默认值是 “0” ,注意是 0,但是他是越小越重要,(意思就是如果要优先显示一个控件,比如tabwidget却要修改上面的FrameLayout,把它设为“1”就可以了)
<FrameLayout
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent">
注意:如果同一个屏幕里的两个控件都是math_parent,它们会根据值的大小来分割屏幕。比如tabwidget的weight是1,FrameLayout的weight是2,那么tab会占屏幕三分之二的大小(越小越重要),Framelayout会占三分之一。
如果有一个设定了大小,比如tab的height是50dp,不会影响占据的大小。