TabHost:
TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget
就是每个tab的标签,FrameLayout则是tab内容。
1、如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置
为@android:id/tabhost
2、TabWidget必须设置android:id为@android:id/tabs
3、FrameLayout需要设置android:id为@android:id/tabcontent
4、参考这儿:
aspx">http://blog.csdn.net/flowingflying/archive/2011/04/06/6304289.a
spx
先自定义一个xml文件:
Java代码
<?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="fill_parent"
android:layout_weight="1.0"
android:paddingBottom="53px"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="50px"
android:visibility="gone"
android:layout_weight="0.0"/>
<RadioGroup
android:gravity="center_vertical"
android:orientation="horizontal"
android:id="@+id/main_radio"
android:background="@drawable/radiogroup_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_gravity="bottom">
<RadioButton
android:id="@+id/main_index_button"
android:layout_marginTop="1.0dip"
android:layout_marginRight="5dip"
android:text="@string/main_name"
android:drawableTop="@drawable/unistall"
style="@style/main_tab_bottom"
android:background="@drawable/radio_bg"/>
<RadioButton
android:id="@+id/main_running_button"
android:layout_marginTop="1.0dip"
android:layout_marginRight="5dip"
android:text="@string/run_manager_name"
android:drawableTop="@drawable/unistall"
style="@style/main_tab_bottom"
android:background="@drawable/radio_bg"/>
<RadioButton
android:id="@+id/main_uninstall_button"
android:layout_marginTop="1.0dip"
android:text="@string/uninstall_manager_name"
android:drawableTop="@drawable/unistall"
style="@style/main_tab_bottom"
android:background="@drawable/radio_bg"/>
</RadioGroup>
</LinearLayout>
</TabHost>
为了让tabHost显示在下方,要将RadioGroup的layout_gravity设置为bottom,
再将FrameLayout的layout_weight设置为1,这样就可以将RadioGroup撑到最下
方。style="@style/main_tab_bottom"里面定义了样式文件。
接下来就是在activity中初始化并添加tabhost:
Java代码
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.addTab(Constant.tabHost.newTabSpec("Main")
.setIndicator(getString(R.string.main_name),null)
.setContent(new Intent(this, Main.class)));
tabHost.addTab(Constant.tabHost.newTabSpec("RunManager")
.setIndicator(getString
(R.string.run_manager_name),null)
.setContent(new Intent(this, RunManager.class)));
tabHost.addTab(Constant.tabHost.newTabSpec
("UninstallManager")
.setIndicator(getString
(R.string.uninstall_manager_name),null)
.setContent(new Intent(this,
UninstallManager.class)));
初始化每个RadioButton并为其添加setOnCheck
菜单:
每个activity包含一个菜单,一个菜单又能包含多个菜单项和多个子菜单,子
菜单其实也是菜单(因为它实现了Menu接口),因此子菜单也可以包含多个菜
单项。OnCreateOptionsMenu()和OnOptionsMenuSelected()是activity中提供
了两个回调方法,用于创建菜单项和响应菜单项的点击
在layout中设置菜单的布局。
在设置activity:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.widget.EditText;
public class MainActivity extends Activity {
// 定义字体大小菜单项的标识
final int FONT_10 = 0x111;
final int FONT_12 = 0x112;
final int FONT_14 = 0x113;
final int FONT_16 = 0x114;
final int FONT_18 = 0x115;
//
final int PLAIN_ITEM = 0x11b;
//
final int FONT_RED = 0X116;
final int FONT_BLUE = 0X117;
final int FONT_GREEN = 0X118;
final int MALE=0x11;
final int FEMALE=0x12;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu fontmenu = menu.addSubMenu("字体大小");
fontmenu.setIcon(R.drawable.a9);
fontmenu.setHeaderIcon(R.drawable.ic_launcher);
fontmenu.setHeaderTitle("设置字体大小");
MenuItem mit1 = fontmenu.add(0, FONT_10, 0, "10号字体
");// 创建菜单项
/*
* mit1.setOnMenuItemClickListener(new
OnMenuItemClickListener() {
*
* @Override public boolean onMenuItemClick(MenuItem
arg0) {
* et.setTextScaleX(10*2); return false; }
*
* });
*/
fontmenu.add(0, FONT_12, 0, "12号字体");
fontmenu.add(0, FONT_14, 0, "14号字体");
fontmenu.add(0, FONT_16, 0, "16号字体");
fontmenu.add(0, FONT_18, 0, "18号字体");
menu.add(0, PLAIN_ITEM, 0, "普通的菜单项");
SubMenu colormenu = menu.addSubMenu("字体颜色");
colormenu.setIcon(R.drawable.a17);
colormenu.setHeaderIcon(R.drawable.ic_launcher);
colormenu.setHeaderTitle("设置字体颜色");
colormenu.setGroupCheckable(0, true, true);//设置菜单
项是否可勾选
colormenu.add(0, FONT_RED, 0, "红色").setCheckable
(true);
MenuItem me2 = colormenu.add(0, FONT_BLUE, 0, "蓝
色");
me2.setCheckable(true);
/*
* me2.setOnMenuItemClickListener(new
OnMenuItemClickListener() {
*
* @Override public boolean onMenuItemClick(MenuItem
arg0) {
* et.setTextColor(Color.BLUE); return false; }
*
* });
*/
colormenu.add(0, FONT_GREEN, 0, "绿色").setCheckable
(true);
SubMenu sexmenu=menu.addSubMenu("您的性别");
sexmenu.setIcon(R.drawable.ic_launcher);
sexmenu.setHeaderIcon(R.drawable.ic_launcher);
sexmenu.setHeaderTitle("请选择您的性别");
sexmenu.add(0,MALE,0,"男");
sexmenu.add(0,FEMALE,0,"女");
sexmenu.setGroupCheckable(0, true, true);
SubMenu openmenu=menu.addSubMenu("打开activity");
openmenu.setIcon(R.drawable.ic_launcher);
openmenu.setHeaderIcon(R.drawable.ic_launcher);
openmenu.setHeaderTitle("请选择要打开的activit");
openmenu.add("firstactivity").setIntent(new Intent
(this,firstActivity.class));
return super.onCreateOptionsMenu(colormenu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case FONT_10:
et.setTextSize(10 * 2);
break;
case FONT_12:
et.setTextSize(12 * 2);
break;
case FONT_14:
et.setTextSize(14 * 2);
break;
case FONT_16:
et.setTextSize(16 * 2);
break;
case FONT_18:
et.setTextSize(18 * 2);
break;
case FONT_RED:
et.setTextColor(Color.RED);
break;
case FONT_BLUE:
et.setTextColor(Color.BLUE);
break;
case FONT_GREEN:
et.setTextColor(Color.GREEN);
break;
}
return true;
}
}
在设置动态菜单:就是在写一个布局和activity:
import android.app.Activity;
import android.os.Bundle;
public class firstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aa);
}
}
重写onCreate方法,用R.layout.aa来调用布局。
TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget
就是每个tab的标签,FrameLayout则是tab内容。
1、如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置
为@android:id/tabhost
2、TabWidget必须设置android:id为@android:id/tabs
3、FrameLayout需要设置android:id为@android:id/tabcontent
4、参考这儿:
aspx">http://blog.csdn.net/flowingflying/archive/2011/04/06/6304289.a
spx
先自定义一个xml文件:
Java代码
<?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="fill_parent"
android:layout_weight="1.0"
android:paddingBottom="53px"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="50px"
android:visibility="gone"
android:layout_weight="0.0"/>
<RadioGroup
android:gravity="center_vertical"
android:orientation="horizontal"
android:id="@+id/main_radio"
android:background="@drawable/radiogroup_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_gravity="bottom">
<RadioButton
android:id="@+id/main_index_button"
android:layout_marginTop="1.0dip"
android:layout_marginRight="5dip"
android:text="@string/main_name"
android:drawableTop="@drawable/unistall"
style="@style/main_tab_bottom"
android:background="@drawable/radio_bg"/>
<RadioButton
android:id="@+id/main_running_button"
android:layout_marginTop="1.0dip"
android:layout_marginRight="5dip"
android:text="@string/run_manager_name"
android:drawableTop="@drawable/unistall"
style="@style/main_tab_bottom"
android:background="@drawable/radio_bg"/>
<RadioButton
android:id="@+id/main_uninstall_button"
android:layout_marginTop="1.0dip"
android:text="@string/uninstall_manager_name"
android:drawableTop="@drawable/unistall"
style="@style/main_tab_bottom"
android:background="@drawable/radio_bg"/>
</RadioGroup>
</LinearLayout>
</TabHost>
为了让tabHost显示在下方,要将RadioGroup的layout_gravity设置为bottom,
再将FrameLayout的layout_weight设置为1,这样就可以将RadioGroup撑到最下
方。style="@style/main_tab_bottom"里面定义了样式文件。
接下来就是在activity中初始化并添加tabhost:
Java代码
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.addTab(Constant.tabHost.newTabSpec("Main")
.setIndicator(getString(R.string.main_name),null)
.setContent(new Intent(this, Main.class)));
tabHost.addTab(Constant.tabHost.newTabSpec("RunManager")
.setIndicator(getString
(R.string.run_manager_name),null)
.setContent(new Intent(this, RunManager.class)));
tabHost.addTab(Constant.tabHost.newTabSpec
("UninstallManager")
.setIndicator(getString
(R.string.uninstall_manager_name),null)
.setContent(new Intent(this,
UninstallManager.class)));
初始化每个RadioButton并为其添加setOnCheck
菜单:
每个activity包含一个菜单,一个菜单又能包含多个菜单项和多个子菜单,子
菜单其实也是菜单(因为它实现了Menu接口),因此子菜单也可以包含多个菜
单项。OnCreateOptionsMenu()和OnOptionsMenuSelected()是activity中提供
了两个回调方法,用于创建菜单项和响应菜单项的点击
在layout中设置菜单的布局。
在设置activity:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.widget.EditText;
public class MainActivity extends Activity {
// 定义字体大小菜单项的标识
final int FONT_10 = 0x111;
final int FONT_12 = 0x112;
final int FONT_14 = 0x113;
final int FONT_16 = 0x114;
final int FONT_18 = 0x115;
//
final int PLAIN_ITEM = 0x11b;
//
final int FONT_RED = 0X116;
final int FONT_BLUE = 0X117;
final int FONT_GREEN = 0X118;
final int MALE=0x11;
final int FEMALE=0x12;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu fontmenu = menu.addSubMenu("字体大小");
fontmenu.setIcon(R.drawable.a9);
fontmenu.setHeaderIcon(R.drawable.ic_launcher);
fontmenu.setHeaderTitle("设置字体大小");
MenuItem mit1 = fontmenu.add(0, FONT_10, 0, "10号字体
");// 创建菜单项
/*
* mit1.setOnMenuItemClickListener(new
OnMenuItemClickListener() {
*
* @Override public boolean onMenuItemClick(MenuItem
arg0) {
* et.setTextScaleX(10*2); return false; }
*
* });
*/
fontmenu.add(0, FONT_12, 0, "12号字体");
fontmenu.add(0, FONT_14, 0, "14号字体");
fontmenu.add(0, FONT_16, 0, "16号字体");
fontmenu.add(0, FONT_18, 0, "18号字体");
menu.add(0, PLAIN_ITEM, 0, "普通的菜单项");
SubMenu colormenu = menu.addSubMenu("字体颜色");
colormenu.setIcon(R.drawable.a17);
colormenu.setHeaderIcon(R.drawable.ic_launcher);
colormenu.setHeaderTitle("设置字体颜色");
colormenu.setGroupCheckable(0, true, true);//设置菜单
项是否可勾选
colormenu.add(0, FONT_RED, 0, "红色").setCheckable
(true);
MenuItem me2 = colormenu.add(0, FONT_BLUE, 0, "蓝
色");
me2.setCheckable(true);
/*
* me2.setOnMenuItemClickListener(new
OnMenuItemClickListener() {
*
* @Override public boolean onMenuItemClick(MenuItem
arg0) {
* et.setTextColor(Color.BLUE); return false; }
*
* });
*/
colormenu.add(0, FONT_GREEN, 0, "绿色").setCheckable
(true);
SubMenu sexmenu=menu.addSubMenu("您的性别");
sexmenu.setIcon(R.drawable.ic_launcher);
sexmenu.setHeaderIcon(R.drawable.ic_launcher);
sexmenu.setHeaderTitle("请选择您的性别");
sexmenu.add(0,MALE,0,"男");
sexmenu.add(0,FEMALE,0,"女");
sexmenu.setGroupCheckable(0, true, true);
SubMenu openmenu=menu.addSubMenu("打开activity");
openmenu.setIcon(R.drawable.ic_launcher);
openmenu.setHeaderIcon(R.drawable.ic_launcher);
openmenu.setHeaderTitle("请选择要打开的activit");
openmenu.add("firstactivity").setIntent(new Intent
(this,firstActivity.class));
return super.onCreateOptionsMenu(colormenu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case FONT_10:
et.setTextSize(10 * 2);
break;
case FONT_12:
et.setTextSize(12 * 2);
break;
case FONT_14:
et.setTextSize(14 * 2);
break;
case FONT_16:
et.setTextSize(16 * 2);
break;
case FONT_18:
et.setTextSize(18 * 2);
break;
case FONT_RED:
et.setTextColor(Color.RED);
break;
case FONT_BLUE:
et.setTextColor(Color.BLUE);
break;
case FONT_GREEN:
et.setTextColor(Color.GREEN);
break;
}
return true;
}
}
在设置动态菜单:就是在写一个布局和activity:
import android.app.Activity;
import android.os.Bundle;
public class firstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.aa);
}
}
重写onCreate方法,用R.layout.aa来调用布局。