如今安卓比較流行的布局就是类似新闻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. hiho一下第131周 后缀自动机二·重复旋律8(循环相似子串)

    后缀自动机五·重复旋律8 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 小Hi ...

  2. 在java代码中设置margin

    我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...

  3. sqlserver 下载地址(SQL Server 2008 R2 中英文 开发版/企业版/标准版 下载)

    转自:http://blog.sina.com.cn/s/blog_624b1f950100pioh.html   注:企业版无法安装在xp和win7,开发版才可以! 一. 简体中文 1. SQL S ...

  4. JAVA生成问答式验证码图片,支持加减算法

    原文:http://liuguihua0823.iteye.com/blog/1511355 import java.awt.Color; import java.awt.Font; import j ...

  5. MailKit---如何知道文件夹下有多少封未读邮件

    如果在mailkit中,文件夹已经选中并打开了的话,那直接使用ImapFolder.Unread属性就可以获取到有多少封未读邮件了. 如果文件夹没有打开,那么你还可以使用查询状态的方法来获取未读状态的 ...

  6. web-51job(前程无忧)-账户、简历-数据库设计

    ylbtech-DatabaseDesgin:web-51job(前程无忧)-账户.简历-数据库设计   1.A,数据库关系图 1.B,数据库设计脚本 /App_Data/1,Account.sql ...

  7. linux文件测试操作

    1.文件测试操作 返回 true 如果... -e 文件存在 -a 文件存在 这个选项的效果与-e 相同.但是它已经被弃用了,并且不鼓励使用 -f file 是一个 regular 文件(不是目录或者 ...

  8. 全双工音频播放器在c#中使用waveIn / waveOut api

    http://www.codeproject.com/Articles/4889/A-full-duplex-audio-player-in-C-using-the-waveIn-w 一篇关于低级音频 ...

  9. EffectiveJava(11)Java中的clone

    java中的clone clone构造器及其静态工厂的变形 优点:它们不依赖于某一种很有风险的,语言之外的对象创建机制; 它们不要求遵守尚未制定好文档的规范 他们不会于final域的正常使用发生冲突 ...

  10. 11. 配置ContextPath【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/51637036 spring boot默认是/ ,这样直接通过http://ip:port/ ...