安卓TabHost+ViewPager+RadioGroup多功能模板整理
如今安卓比較流行的布局就是类似新闻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多功能模板整理的更多相关文章
- 安卓---Tabhost实现页面局部刷新--父页子页之间的传值
TabHost的实现分为两种,一个是不继承TabActivity,一个是继承自TabActivity:当然了选用继承自TabActivity的话就相对容易一些,下面来看看分别是怎样来实现的吧. 我只写 ...
- android中TabHost和RadioGroup
android底部菜单应用 博客分类: android--UI示例 TabHostMenuRadioGroupButton 在android中实现菜单功能有多种方法. Options Menu:用户 ...
- Android底部导航栏创建——ViewPager + RadioGroup
原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...
- 字符串系列——KMP模板整理
KMP模板整理 KMP与扩展KMP: /*vs 2017/ vs code以外编译器,去掉windows.h头文件和system("pause");*/ #include<i ...
- count_if 功能模板
count_if 功能模板 template <class InputIterator, class UnaryPredicate> typename iterator_traits< ...
- ACM算法模板整理
史诗级ACM模板整理 基本语法 字符串函数 istream& getline (char* s, streamsize n ); istream& getline (char* s, ...
- Sed常用功能个人整理
Sed常用功能个人整理 AsdilFibrizo关注 2019.06.24 10:23:41字数 240阅读 15 Sed对1G以下的数据效率很高这里介绍一些个人在工作中遇到的sed问题 1.查找字段 ...
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
1.说明: 在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源码+断点调试解决了一些碰到的问题,写一篇博客总结一下,有同样需求的朋友可以借鉴一下,自己以后有用 ...
- 21 ViewPager RadioGroup
结构 MainActivity.java package com.qf.day21_viewpagerfragmentrg_demo4; import java.util.ArrayList; imp ...
随机推荐
- 在VisualStudio 工具箱中隐藏用户控件
当我们创建一个用户控件后,VisualStudio会自动将其添加到工具箱中,本来这是一个比较贴心的设计.但是,有的时候,我们并不想将用户控件放到工具箱中. 例如:在WPF中,为了避免一个页面的控件过多 ...
- 使用Chrome DevTools直接调试Node.js与JavaScript(并行)
Good News: 现在我们可以用浏览器调试node.js了!!! 前提 Node.js 6.3+, 这个可上Node.js官网自行下载: Chrome 55+. 如果您本地的chrome升级到最新 ...
- redis--AOF
Redis 分别提供了 RDB 和 AOF 两种持久化机制: RDB 将数据库的快照( snapshot)以二进制的方式保存到磁盘中. 相当于MySQL binlog 的 raw模式 AOF 则以协议 ...
- bmp,jpg,png,tif,wmf,emf与eps图片格式转换
wmf/emf是两种Microsoft Windows的图形文件格式.它是一个矢量图格式,但是也允许包含位图.本质上,一个WMF文件保存一系列可以用来重建图片的Windows GDI命令.在某种程度上 ...
- iOS_block代码块
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 系统封装 如何为原生PE集成软件
1 我们首先集成Explorer.老外的BSExplorer比较好用,下载之后得到这些文件,不算太大. 2 这里需要注意,前一章讲解如何打造原生PE已经制作成了ISO,这里想要集成软件还需要回到刚 ...
- C++003基础
1.C++对C的扩展 1简单的C++程序 1.1求圆的周长和面积 数据描写叙述: 半径.周长,面积均用实型数表示 数据处理: 输入半径 r. 计算周长 = 2*π*r : 计算面积 = π* r2 . ...
- magento安装wordpress
说起来事实上很easy,如今简诉一下安装过程 1.到magento connect界面安装magento扩展,地址:http://www.magentocommerce.com/magento-con ...
- 非阻塞socket中read、write返回值
read返回值 >0 读取数据的长度 =0 接收到对端发送的FIN,表示对端的写端关闭. <0 如果errno=EINTR.收到信号并从信号处理函数返回时,慢系统调用会返回并设 ...
- spring 源码下载
github spring 源码 导入Spring源码方法 java世界中的三大构建工具:ant,maven,gradle gradle 简介