如今安卓比較流行的布局就是类似新闻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. 集合框架(06)Arrays

    Arrays Arrays:用于操作数组的工具类,里面都是静态方法 ---数组变集合 1.asList:将数组变成List集合 把数组变成list集合的好处?可以使用集合的思想和方法来操作数组中的元素 ...

  2. MSSQL收缩事务日志&日志文件过大无法收缩

    原文:MSSQL收缩事务日志&日志文件过大无法收缩 一.MS SQL SERVER 2005 --1.清空日志 exec('DUMP TRANSACTION 数据库名 WITH NO_LOG' ...

  3. SpannableString 转换局部字体大小,但在EditText测量之前设置内容,测量高度为,字体变小之前的高度

    public void setHint(@NonNull String hint, @Nullable CharSequence subHint) { this.hint = hint; if (su ...

  4. 关联模型中如果condition条件

    在练习中,有一个user表和地址表,一对多的关系. 我的想法是,通过这个关联模型找出这个用户下面默认值字段为1的地址 控制器中 public function index(){ $User = D(' ...

  5. perl一次读取多行文本的策略

    在处理文本时,经常遇到这种情况:就是我们须要把两行文本做一个比較,然后选择性输出. 而在while(<FILEHAND>){do something}程序块中默认仅仅能一次读取一行.笔者在 ...

  6. java 文件上传数据库

    存储文件的数据库类型: 1.oracle :Blob,bfile类型 2.mysql:longblob类型 3.sqlserver :varbinary(Max)类型 文件都是以二进制流存入数据库的, ...

  7. 新人补钙系列教程之:AS3 位运算符

    ECMAScript 整数有两种类型,即有符号整数(允许用正数和负数)和无符号整数(只允许用正数).在 ECMAScript 中,所有整数字面量默认都是有符号整数,这意味着什么呢? 有符号整数使用 3 ...

  8. Linux&Windows下批量修改文件后缀

    Linux下从给定文件夹中找出小于1M的文件,并批量添加.gif后缀 先看一下文件夹下的目录的格式 ll -Sh -rw-rw-r-- 1 yangkun yangkun 17M May 10 15: ...

  9. Win7下nginx默认80端口被System占用,造成nginx启动报错的解决方案

    Win7下nginx默认80端口被System占用,造成nginx启动报错的解决方案   在win7 32位旗舰版下,启动1.0.8版本nginx,显示如下错误:  [plain] 2012/04/0 ...

  10. 转: java web demo的示例

    http://quqtalk.iteye.com/blog/360699 从事Java开发已经两年了,但是由于工作的关系,对Java Web还是个freshman.今天做了一个Java Web的简单D ...