如今安卓比較流行的布局就是类似新闻client和手机QQ那种的底端可选择,上面的个别页面能够滑动选择。

在測试过程中发现用安卓自带的TabHost去构建。非常难得到自己定义的效果。

因此採用TabHost+ViewPager+RadioGroup去构建这个效果

首先要弄清楚各自的用途和功能

(1)TabHost

因为安卓自带的TabHost貌似在有些手机版本号上仅仅能固定在底端的位置,所以我们用GadioGroup去显示界面button,因为构建HabHost必须定义host、tabs、content几个内容,这样我们隐藏tabs。用GadioGroup取代显示。代码例如以下:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<span style="color:#ff0000;"> android:id="@android:id/tabhost" </span>
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android_mode.MainActivity" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <FrameLayout
<span style="color:#ff0000;">android:id="@android:id/tabcontent"</span>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout> <TabWidget
<span style="color:#ff0000;">android:id="@android:id/tabs"</span>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<span style="color:#000066;">android:visibility="gone"</span> >
</TabWidget> <RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <RadioButton
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#00ABCD"
android:button="@null"
android:gravity="center"
android:text="首页" /> <View
android:layout_width="2px"
android:layout_height="fill_parent" /> <RadioButton
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#00ABCD"
android:button="@null"
android:gravity="center"
android:text="设置" />
</RadioGroup>
</LinearLayout> </TabHost>

对于主文件,因为在安卓3.0版本号下面不支持TabActivity。因此我们考虑到兼容性,选用ActivityGroup。

其详细方法例如以下所看到的:

public class MyTabOwnAct extends ActivityGroup {

	RadioButton radioButton1;
RadioButton radioButton2;
TabHost tabHost; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmain);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.setup(this.getLocalActivityManager()); //必需要又这个语句。假设继承TabActivity可不要
tabHost.addTab(tabHost.newTabSpec("l1")
.setContent(new Intent(this, Act1.class)).setIndicator("首页"));
tabHost.addTab(tabHost.newTabSpec("l2")
.setContent(new Intent(this, Act2.class)).setIndicator("设置"));
radioButton1 = (RadioButton) findViewById(R.id.b1);
radioButton2 = (RadioButton) findViewById(R.id.b2);
radioButton1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tabHost.setCurrentTabByTag("l1"); //显示与隐藏的标记
}
});
radioButton2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
tabHost.setCurrentTabByTag("l2");
}
});
}
}

到眼下为止我们把底部的效果实现,即能够通过以下的button,改变上面的界面。

接下来我们选择上面当中的一个界面,用ViewPager实现滑动的效果。

界面布局文件例如以下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#FF0fab"
android:gravity="center"
android:text="页面一" /> <View
android:layout_width="2dp"
android:layout_height="fill_parent" /> <TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#da0ccb"
android:gravity="center"
android:text="页面二" /> <View
android:layout_width="2dp"
android:layout_height="fill_parent" /> <TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#eeffff"
android:gravity="center"
android:text="页面三" />
</LinearLayout> <android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager> </LinearLayout>
</pre><span style="font-size:18px">我们要准备三个界面的布局。这里我分别命名为p1.xml,p2.xml,p3.xml,然后我们在主文件里通过得到ViewPager后关联到适配器,就可以得到滑动的效果。</span><pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">public class Act1 extends Activity {</span>
	ViewPager pager;

	@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pager);
pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new MyPagerAdapter());
} class MyPagerAdapter extends PagerAdapter {
List<View> list = new ArrayList<View>(); public MyPagerAdapter() {
// TODO Auto-generated constructor stub
View view1 = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.p1, null);
View view2 = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.p2, null);
View view3 = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.p3, null);
list.add(view1);
list.add(view2);
list.add(view3); } @Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
((ViewPager) container).addView(list.get(position));
return list.get(position);
} @Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
((ViewPager) container).removeView(list.get(position));///(position);// (list.get(position));
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
} }
}

效果显示图:

安卓TabHost+ViewPager+RadioGroup多功能模板整理的更多相关文章

  1. 安卓---Tabhost实现页面局部刷新--父页子页之间的传值

    TabHost的实现分为两种,一个是不继承TabActivity,一个是继承自TabActivity:当然了选用继承自TabActivity的话就相对容易一些,下面来看看分别是怎样来实现的吧. 我只写 ...

  2. android中TabHost和RadioGroup

    android底部菜单应用 博客分类: android--UI示例 TabHostMenuRadioGroupButton  在android中实现菜单功能有多种方法. Options Menu:用户 ...

  3. Android底部导航栏创建——ViewPager + RadioGroup

    原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...

  4. 字符串系列——KMP模板整理

    KMP模板整理 KMP与扩展KMP: /*vs 2017/ vs code以外编译器,去掉windows.h头文件和system("pause");*/ #include<i ...

  5. count_if 功能模板

    count_if 功能模板 template <class InputIterator, class UnaryPredicate> typename iterator_traits< ...

  6. ACM算法模板整理

    史诗级ACM模板整理 基本语法 字符串函数 istream& getline (char* s, streamsize n ); istream& getline (char* s, ...

  7. Sed常用功能个人整理

    Sed常用功能个人整理 AsdilFibrizo关注 2019.06.24 10:23:41字数 240阅读 15 Sed对1G以下的数据效率很高这里介绍一些个人在工作中遇到的sed问题 1.查找字段 ...

  8. ViewPager+RadioGroup实现标题栏切换,Fragment切换

    1.说明: 在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源码+断点调试解决了一些碰到的问题,写一篇博客总结一下,有同样需求的朋友可以借鉴一下,自己以后有用 ...

  9. 21 ViewPager RadioGroup

    结构 MainActivity.java package com.qf.day21_viewpagerfragmentrg_demo4; import java.util.ArrayList; imp ...

随机推荐

  1. 「kuangbin带你飞」专题十八 后缀数组

    layout: post title: 「kuangbin带你飞」专题十八 后缀数组 author: "luowentaoaa" catalog: true tags: - kua ...

  2. Broken Code

    给一个sorted array 0 0 0 1 1 1 1,然后找出第一个1的位置. 边界情况:array为空或者全0. 思路:二分查找.为了优化,可以先判断最后一个数是不是0. class Solu ...

  3. oracle中执行execute的时候报异常ORA-01031的解决办法

    在做实验的时候,编写关于"在存储过程中使用动态sql,建立一个统计表,并把统计结果,插入这个表中"的PL/sql语句在执行时出现权限不足的问题. 上网查询很多,看到了下面这篇博文( ...

  4. iOS静态库打包

    当了三年多程序员,第一次写随便,说来有些惭愧.想成为一个优秀的程序员,分析,思考,总结并且做记录是必不可少的,今天先从简单的命令总结记起.好了,废话不多说了,Let's start our games ...

  5. ios中调用WCF

    例子比较简单 记录下思路 1.接口中定义 实体和方法声明 //登录信息        [OperationContract]        [WebInvoke(UriTemplate = " ...

  6. office2010使用mathtype时,出现未找到MathPage.WLL解决方案--亲测有用

    安装mathtype时,出现如下错误: 解决方案: 参考此网址中的内容:http://www.mathtype.cn/wenti/word-jianrong.html 首先需要找到在Word加载的两个 ...

  7. Linux用户配置sudo权限(visudo)

    sudo的工作过程如下: 1,当用户执行sudo时,系统会主动寻找/etc/sudoers文件,判断该用户是否有执行sudo的权限 2,确认用户具有可执行sudo的权限后,让用户输入用户自己的密码确认 ...

  8. INTZ DX format

    http://aras-p.info/texts/D3D9GPUHacks.html 格式 用法 资源 描述 NVIDIA GeForce AMD Radeon 英特尔 阴影映射 D3DFMT_D16 ...

  9. C++中的类所占内存空间+继承中的成员访问控制

    C++学习之继承中的成员访问控制 C++中的类所占内存空间总结

  10. 用new和delete运算符进行动态分配和撤销存储空间

    測试描写叙述:暂时开辟一个存储空间以存放一个结构体数据 #include <iostream> #include <string> using namespace std; s ...