android中的tab小结
16lz
2021-12-04
android中的tab,其实很简单,几个步骤
1 首先是一个布局文件,一般是framelayout中有多个linarlayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/widget_layout_Blue"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText android:id="@+id/widget34" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="EditText"
android:textSize="18sp">
</EditText>
<Button android:id="@+id/widget30" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button">
</Button>
</LinearLayout>
<LinearLayout android:id="@+id/widget_layout_red"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<AnalogClock android:id="@+id/widget36"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</AnalogClock>
</LinearLayout>
<LinearLayout android:id="@+id/widget_layout_green"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<RadioGroup android:id="@+id/widget43"
android:layout_width="166px" android:layout_height="98px"
android:orientation="vertical">
<RadioButton android:id="@+id/widget44"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
<RadioButton android:id="@+id/widget45"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
</RadioGroup>
</LinearLayout>
</FrameLayout>
2 实际代码:
public class MyTab extends TabActivity implements OnTabChangeListener {
private TabHost myTabhost;
}
注意这里是继承TabActivity了,之后的TabHost用来容纳每一个标签页,还可以包含任何类型的视图.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
myTabhost=this.getTabHost();
myTabhost.addTab(myTabhost.newTabSpec("One").setIndicator("A",getResources().getDrawable(R.drawable.gimp)).setContent(R.id.widget_layout_Blue));
.........
myTabhost.setCurrentTab(0);
这里,addTab()用来给TabHost上添加一个页;
newTabSpec方法的参数指定该TAB项的ID值得,当在不同的tab切换时,TABHOST会发送不同的ontabchange消息,消息的参数就是该id值.
setIndicator用于指定标签页上显示的内容,还可以加图标.
setcontent()用来指定该tab页对应的视图.
对事件的响应:
public void onTabChanged(String tagString) {
// TODO Auto-generated method stub
if (tagString.equals("One")) {
myMenuSettingTag = 1;
Toast.makeText(getApplicationContext(), "tabid="+tagString,Toast.LENGTH_LONG).show();
}
1 首先是一个布局文件,一般是framelayout中有多个linarlayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/widget_layout_Blue"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText android:id="@+id/widget34" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="EditText"
android:textSize="18sp">
</EditText>
<Button android:id="@+id/widget30" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button">
</Button>
</LinearLayout>
<LinearLayout android:id="@+id/widget_layout_red"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<AnalogClock android:id="@+id/widget36"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</AnalogClock>
</LinearLayout>
<LinearLayout android:id="@+id/widget_layout_green"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<RadioGroup android:id="@+id/widget43"
android:layout_width="166px" android:layout_height="98px"
android:orientation="vertical">
<RadioButton android:id="@+id/widget44"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
<RadioButton android:id="@+id/widget45"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
</RadioGroup>
</LinearLayout>
</FrameLayout>
2 实际代码:
public class MyTab extends TabActivity implements OnTabChangeListener {
private TabHost myTabhost;
}
注意这里是继承TabActivity了,之后的TabHost用来容纳每一个标签页,还可以包含任何类型的视图.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
myTabhost=this.getTabHost();
myTabhost.addTab(myTabhost.newTabSpec("One").setIndicator("A",getResources().getDrawable(R.drawable.gimp)).setContent(R.id.widget_layout_Blue));
.........
myTabhost.setCurrentTab(0);
这里,addTab()用来给TabHost上添加一个页;
newTabSpec方法的参数指定该TAB项的ID值得,当在不同的tab切换时,TABHOST会发送不同的ontabchange消息,消息的参数就是该id值.
setIndicator用于指定标签页上显示的内容,还可以加图标.
setcontent()用来指定该tab页对应的视图.
对事件的响应:
public void onTabChanged(String tagString) {
// TODO Auto-generated method stub
if (tagString.equals("One")) {
myMenuSettingTag = 1;
Toast.makeText(getApplicationContext(), "tabid="+tagString,Toast.LENGTH_LONG).show();
}
更多相关文章
- android中的tab小结
- android 布局xml中 android:fitsSystemWindows="true"的作用
- view属性大全
- Android进阶——Android视图工作机制之measure、layout、draw
- android最佳实践(二)
- cocos2dx打包Android出现“未指定Android目标平台版本”问题
- Mono for Android(安卓)实现高效的导航
- android中view的一些属性
- 高级组件之网格视图