浅谈Android选项卡(二)
前面简单介绍了选项卡,下面以及后面的几篇文章介绍下Android选项卡的几种简单实现方法。
http://blog.csdn.net/xia215266092/article/details/9613897
看到上面的最版本的QQ软件,整个软件的UI框架就是选项卡,一般想到的就是使用Android自带的TabActivity实现。
实现需要一个主界面,来存放选项卡,在布局中需要存放TabHost和TabWidget。
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="1dip"
android:paddingRight="1dip"
android:paddingTop="4dip"
android:visibility="gone" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f00"
android:orientation="horizontal" > <RadioButton
android:id="@+id/rb01"
style="@style/menu_item"
android:checked="true"
android:drawableTop="@drawable/menu_home"
android:text="主页" /> <RadioButton
android:id="@+id/rb02"
style="@style/menu_item"
android:drawableTop="@drawable/menu_clear"
android:text="清除" /> <RadioButton
android:id="@+id/rb03"
style="@style/menu_item"
android:drawableTop="@drawable/menu_refresh"
android:text="刷新" /> <RadioButton
android:id="@+id/rb04"
style="@style/menu_item"
android:drawableTop="@drawable/menu_save"
android:text="保存" /> <RadioButton
android:id="@+id/rb05"
style="@style/menu_item"
android:drawableTop="@drawable/menu_more"
android:text="更多" />
</RadioGroup> </LinearLayout> </TabHost>
同时需要个Activity,这个Activity必须继承TabActivity,其实也不一定需要继承这个,可以继承ActivityGroup,自定义一个tabactivity,但是这样比较复杂,所以我们还是继承TabActivity吧。
public class TabMainActivity extends TabActivity {
TabHost tab;
Context context;
RadioGroup rg;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_main);
tab = getTabHost();
context = this;
tab.addTab(tab.newTabSpec("A").setIndicator("A").setContent(new Intent(context, AActivity.class)));
tab.addTab(tab.newTabSpec("B").setIndicator("B").setContent(new Intent(context, BActivity.class)));
tab.addTab(tab.newTabSpec("C").setIndicator("C").setContent(new Intent(context, CActivity.class)));
tab.addTab(tab.newTabSpec("D").setIndicator("D").setContent(new Intent(context, DActivity.class)));
tab.addTab(tab.newTabSpec("E").setIndicator("E").setContent(new Intent(context, EActivity.class)));
rg = (RadioGroup) findViewById(R.id.rg);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
int idx = -1;
if (checkedId == R.id.rb01) {
idx = 0;
} else if (checkedId == R.id.rb02) {
idx = 1;
} else if (checkedId == R.id.rb03) {
idx = 2;
} else if (checkedId == R.id.rb04) {
idx = 3;
} else if (checkedId == R.id.rb05) {
idx = 4;
}
switchActivity(idx);
}
});
}
protected void switchActivity(int idx) {
int n = tab.getCurrentTab();
if (idx < n) {
tab.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_out));
} else if (idx > n) {
tab.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_out));
}
tab.setCurrentTab(idx);
if (idx < n) {
tab.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_in));
} else if (idx > n) {
tab.getCurrentView().startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_in));
}
RadioButton rb = (RadioButton) rg.getChildAt(idx);
rb.setChecked(true);
}
private Map<String, Object> data = new HashMap<String, Object>();
protected void put(String key, String value) {
data.put(key, value);
}
protected Object get(String key) {
return data.get(key);
}
}
AActivity,BActivity,CActivity,DActivity,EActivity,都是点击选项卡,对应的Activity,这样UI框架,可以按照软件的功能来分模块,便于团队人员的合作和开发。
底部的菜单使用的RadioGroup,因在RadioGroup里面的有若干RadioButton,只有能有一个才能被选中,这样就很完美的实现的上面被选中的效果,Android有自定义Radiobutton选中的背景。
自定义一个Radiobutton
<RadioButton
android:id="@+id/rb01"
style="@style/menu_item"
android:checked="true"
android:drawableTop="@drawable/menu_home"
android:text="主页" />
然后在自定义一个style,让RadioButtong使用使用这个Style。
<style name="menu_item">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:padding">10dp</item>
<item name="android:button">@null</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/menu_item_bg</item>
<item name="android:layout_weight">1</item>
</style>
<item name="android:background">@drawable/menu_item_bg</item>
就是这个RadioButton的背景。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:state_checked="false" android:drawable="@drawable/menu_normal" />
<item android:state_checked="true" android:drawable="@drawable/menu_press" />
<item android:state_pressed="true" android:drawable="@drawable/menu_press" />
</selector>
定义不同状态的显示的背景,当选中或者是被点击的时候,使用的背景menu_press,否则就是menu_normal。
好了,这篇先简单介绍在这里,后面还有后续。
浅谈Android选项卡(二)的更多相关文章
- 浅谈Android选项卡(四)
前面几篇介绍的选项的用法,基本上使用TabActivity.ViewPager.已经基本上满足开发需求了.但是这里再介绍一种小技巧,在有的时候,感觉使用前面的ViewPager和Fragment时候, ...
- 浅谈Android选项卡(一)
选项卡,这样UI设计在很多方面都存在,window,web,ios,Android. 选项卡的主要作用,不用多介绍,可以在有线的空间内,显示出更多内容,同时也是操作起来也很方便.
- 浅谈Android选项卡(三)
上一节介绍了TabActivity的简单用法,但是现在的Api中已经不建议使用了,建议使用Fragment来替代以上的功能,下面介绍下使用Fragment和ViewPager的结合使用. http:/ ...
- [转]浅谈Android五大布局(二)——RelativeLayout和TableLayout
在浅谈Android五大布局(一)中已经描述了LinearLayout(线性布局).FrameLayout(单帧布局)和AbsoulteLayout(绝对布局)三种布局结构,剩下的两种布局Relati ...
- 浅谈Android应用保护(一):Android应用逆向的基本方法
对于未进行保护的Android应用,有很多方法和思路对其进行逆向分析和攻击.使用一些基本的方法,就可以打破对应用安全非常重要的机密性和完整性,实现获取其内部代码.数据,修改其代码逻辑和机制等操作.这篇 ...
- 安卓开发_浅谈Android动画(四)
Property动画 概念:属性动画,即通过改变对象属性的动画. 特点:属性动画真正改变了一个UI控件,包括其事件触发焦点的位置 一.重要的动画类及属性值: 1. ValueAnimator 基本属 ...
- 浅谈Android应用性能之内存
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 文/ jaunty [博主导读]在Android开发中,不免会遇到许多OOM现象,一方面可能是由于开 ...
- 浅谈Android五大布局
Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLay ...
- 浅谈Kotlin(二):基本类型、基本语法、代码风格
浅谈Kotlin(一):简介及Android Studio中配置 浅谈Kotlin(二):基本类型.基本语法.代码风格 浅谈Kotlin(三):类 浅谈Kotlin(四):控制流 通过上面的文章,在A ...
随机推荐
- 【2018沈阳赛区网络预选赛J题】Ka Chang【分块+DFS序+线段树】
题意 给出一个有根树(根是1),有n个结点.初始的时候每个结点的值都是0.下面有q个操作,操作有两种,操作1.将深度为L的点的值全部增加X.操作2.查询以x为根的子树的结点值得和. 其中N,Q< ...
- 653. Two Sum IV - Input is a BST 二叉树版本
[抄题]: Given a Binary Search Tree and a target number, return true if there exist two elements in the ...
- Windows服务器常用的性能计数器
Windows常用性能计数器总结 基础监控: 1.SQL Server Buffer: Buffer Cache Hit Ratio 这是一个很重要查看内存是否不足的参数.SQL Server Buf ...
- SQL Compare数据库版本比较工具
Red Gate系列文章: Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程 Red Gate系列之二 SQL Source C ...
- Python使用signal模块实现定时执行
在liunx系统中要想每隔一分钟执行一个命令,最普遍的方法就是crontab了,如果不想使用crontab,经同事指点在程序中可以用定时器实现这种功能,于是就开始摸索了,发现需要一些信号的知识... ...
- AID-应用标识符的组成规则
AID:即唯一标识一个应用,分为两部分,RID(5字节)+PIX(最多11字节) RID:注册标识符,由ISO组织来分配,标识一个全球唯一的应用提供商,一般是分配给卡组织,比如分配给Master,比如 ...
- [转]修改github已提交的用户名和邮箱
改变作者信息 为改变已经存在的 commit 的用户名和/或邮箱地址,你必须重写你 Git repo 的整个历史. 警告:这种行为对你的 repo 的历史具有破坏性.如果你的 repo 是与他人协同工 ...
- .NET开源MSSQL、Redis监控产品Opserver之MSSQL配置
MSSQL的配置比较简单,主要包括三部分: 默认配置(defaultConnectionString).集群配置(clusters).单实例配置(instances) defaultConnectio ...
- Dubbo的配置及启动
Tomcat+Dubbo安装 1.将tomcat的webapps目录下的所有文件清空,讲Dubbo管理控制台的程序dubbo-admin-2.5.3.war放 到webapps中,并且解压命名为ROO ...
- javascript总结26:Date
1 获取Date对象 Date-引用类型,JavaScript中的内置对象 获取当前时间 var date = new Date(); //UTC的时间 //返回数字,时间的毫秒形式 var date ...