仿QQ底部切换(Fragment + Radio)
第一步: activity_main.xml 布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" > //activity_main.xml <FrameLayout
android:id="@+id/frame_container" //主内容的区域
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/div_view" >
</FrameLayout> <View
android:id="@+id/div_view"
android:layout_width="fill_parent"
android:layout_height="1.0px"
android:layout_above="@+id/main_radiogroup" //横线 在radiogroup的上面
android:layout_marginBottom="2dip"
android:background="#ffc9cacb" /> <RadioGroup
android:id="@+id/main_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" //在父布局 的下面
android:orientation="horizontal" > <RadioButton
android:id="@+id/tab_rbo_question"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" //占 一
android:background="@null"
android:button="@null"
android:checked="true" // 默认选中
android:drawableTop="@drawable/selector_tab_question" //"问他" 图片的选择器
android:text="问他"
android:textColor="@color/tv_checked_bg" /> <RadioButton
android:id="@+id/tab_rbo_message"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/selector_tab_message" // "消息" 图片选择器
android:gravity="center_horizontal" //居中
android:text="消息" /> <RadioButton
android:id="@+id/tab_rbo_answer"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/selector_tab_answer" //"我要回答" 图片选择器器
android:gravity="center_horizontal"
android:text="我要回答" /> <RadioButton
android:id="@+id/tab_rbo_discover"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/selector_tab_discover" //"发现" 图片选择器
android:gravity="center_horizontal"
android:text="发现" /> <RadioButton
android:id="@+id/tab_rbo_user"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/selector_tab_user" //"我" 图片选择器
android:gravity="center_horizontal"
android:text="我" />
</RadioGroup> </RelativeLayout>
drawable目录下 selector_tab_question.xml(" 问他"选择器)
<?xml version="1.0" encoding="utf-8"?> //selector_tab_question
<selector xmlns:android="http://schemas.android.com/apk/res/android"
<item android:drawable="@drawable/tab_question_select" android:state_checked="true"></item>
<item android:drawable="@drawable/tab_question_select" android:state_pressed="true"></item>
<item android:drawable="@drawable/tab_question_nor"></item>
</selector>
.... 其他几个选择器 也一样
style.xml 下 name ="tab_textview"
<style name="tab_textview">
<item name="android:textSize">11sp</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:drawablePadding">2dip</item>
</style>
第二步 MainActivity.java
public class MainActivity extends FragmentActivity {
/** 底部导航 */
private RadioGroup main_radiogroup;
private Fragment mCurrent; // 当前的mCurrent
private TabFragment faxian; // 发现
private TabFragment huida; // 回答
private TabFragment denglu; // 登陆
private TabFragment xiaoxi; // 消息
private TabFragment wenta; // 问他 // private FragmentManager fm;
// private FragmentTransaction ft; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_radiogroup = (RadioGroup) this.findViewById(R.id.main_radiogroup);
// fm = getSupportFragmentManager();
// main_radiogroup 被选中 ,时候的监听器
main_radiogroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() { @SuppressLint("ResourceAsColor")
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
/** 改变文字的颜色 */
int length = group.getChildCount();
for (int i = 0; i < length; i++) {
RadioButton radioButton = (RadioButton) group
.getChildAt(i);
if (radioButton.getId() != checkedId) {
// 没有选中的 时候的文字颜色
radioButton.setTextColor(getResources()
.getColor(R.color.tv_checked_nor));
} else { // 选中的时候的文字颜色
radioButton.setTextColor(getResources()
.getColor(R.color.tv_checked_bg));
}
}
switch (checkedId) {
case R.id.tab_rbo_answer:
changeAnswer(); //切换回答
break;
case R.id.tab_rbo_discover:
changeDiscover(); //切换问他
break;
case R.id.tab_rbo_message:
changeMessage(); //消息
break;
case R.id.tab_rbo_question:
changeQuesion();
break;
case R.id.tab_rbo_user: //用户
changeUser();
break;
}
}
});
mCurrent = new TabFragment(); //起始时候 给个值 ,不然会 报异常
/** 默认选择第一个 */
changeQuesion();
} /**
* 切换问他
*/
private void changeQuesion() {
if (wenta == null) {
wenta = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "问他");
wenta.setArguments(bundle);
}
switchContent(wenta);
// ft.replace(R.id.frame_container, tab1);
// ft.commit(); } /**
* 切换消息
*/
private void changeMessage() {
if (xiaoxi == null) {
xiaoxi = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "消息");
xiaoxi.setArguments(bundle);
}
switchContent(xiaoxi);
// ft.replace(R.id.frame_container, tab1);
// ft.commit();
} /***
* 切换登录
*/
private void changeUser() {
if (denglu == null) {
denglu = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "用户");
denglu.setArguments(bundle);
}
switchContent(denglu);
// ft.replace(R.id.frame_container, tab1);
// ft.commit();
} /***
* 切换回答
*/
private void changeAnswer() {
if (huida == null) {
huida = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "回答");
huida.setArguments(bundle);
}
switchContent(huida);
// ft.replace(R.id.frame_container, tab1);
// ft.commit();
} /***
* 切换发现
*/
private void changeDiscover() {
if (faxian == null) {
faxian = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "发现");
faxian.setArguments(bundle);
}
switchContent(faxian);
// ft.replace(R.id.frame_container, tab1);
// ft.commit();
} /** 修改显示的内容 不会重新加载 **/
public void switchContent(Fragment to) {
if (mCurrent != to) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
if (!to.isAdded()) { // 先判断是否被add过
transaction.hide(mCurrent).add(R.id.frame_container, to)
.commit(); // 隐藏当前的fragment,add下一个到Activity中
} else {
transaction.hide(mCurrent).show(to).commit(); // 隐藏当前的fragment,显示下一个
}
mCurrent = to;
}
// showContent();
}
}
TabFragment.java
public class TabFragment extends Fragment {
private String tab_name;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//得到 activity 传来的 值
tab_name=getArguments().getString("name"); } @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_tab, container, false);
TextView tv=(TextView) view.findViewById(R.id.textview1);
tv.setText(tab_name);
return view;
}
}
layout文件夹下 fragment_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > //fragment_tab <TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="100sp"
android:layout_centerInParent="true" // 居 父中间
/> </RelativeLayout>
运行后效果:
仿QQ底部切换(Fragment + Radio)的更多相关文章
- 仿qq底部的提示标记
看到一个比較不错的开源项目,分享给大家: <?xml version="1.0" encoding="utf-8"?> <RelativeLa ...
- Android高仿qq及微信底部菜单的几种实现方式
最近项目没那么忙,想着开发app的话,有很多都是重复,既然是重复的,那就没有必要每次都去写,所以就想着写一个app通用的基本框架,这里说的框架不是什么MVC,MVP,MVVM这种,而是app开发的通用 ...
- Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...
- Fragment,仿QQ空间
转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9023451 在今天的这篇文章当中,我依然会以实战加理论结合 ...
- Android应用经典主界面框架之中的一个:仿QQ (使用Fragment, 附源代码)
备注:代码已传至https://github.com/yanzi1225627/FragmentProject_QQ 欢迎fork,如今来审视这份代码,非常多地方写的不太好,欢迎大家指正.有时间我会继 ...
- jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动
jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动 http://www.17sucai.com/pins/demoshow/382
- Activity内切换fragment实现底部菜单切换遇到的坑
1.一般说来,app底部导航都会设计为5个菜单,可以使用textView,也可使用radioButton,这里我选择用radioButton,给radioButton直接设置selector就可以实现 ...
- Android 仿QQ首页的消息和电话的切换,首页的头部(完全用布局控制)
Android 仿QQ首页的消息和电话的切换,首页的头部(完全用布局控制) 首先贴上七个控制布局代码 1.title_text_sel.xml 字体颜色的切换 放到color文件夹下面 <?xm ...
- android-改进<<仿QQ>>框架源代码
该文章主要改动于CSDN某大神的一篇文章,本人认为这篇文章的面向对象非常透彻,以下分享例如以下可学习的几点: Android应用经典主界面框架之中的一个:仿QQ (使用Fragment, 附源代码) ...
随机推荐
- ubunbu退出nano
无意中进入ubuntu GNU nano ctrl+x即可退出
- mysql 历史数据表迁移方案
当业务运行一段时间后,会出现有些表数据量很大,可能对系统性能产生不良的影响,常见的如订单表.登录log表等,这些数据很有时效性,比如我们一般很少去查上个月的订单,最多也就是报表统计会涉及到. 在我们的 ...
- Spring Cloud之Eureka自我保护环境搭建
Eureka详解 服务消费者模式 获取服务 消费者启动的时候,使用服务别名,会发送一个rest请求到服务注册中心获取对应的服务信息,让后会缓存到本地jvm客户端中,同时客户端每隔30秒从服务器上更新一 ...
- Native App vs Web App 以及 Hybrid App的实现原理
移动应用基本的三种类型 1) Native 应用程序 2) Web 应用程序 3) 混合应用程序(Hybrid: Native应用和web应用结合) Native 应用 直接运行在电脑上或者智能 ...
- 《java编程思想》:散列的原理
以实现一个简单的HashMap为例,详细讲解在code之中. 简单解释散列原理: 1.map中内建固定大小数组,但是数组并不保存key值本身,而是保存标识key的信息 2.通过key生成数组角标,对应 ...
- javascript获取窗口位置、绝对位置、事件位置等
有段时间没更新博客了,工作实在太忙了,加班加班再加班就是我们这个行业的常态吧...还好最近把工作进度完成了,终于有些空余时间了.关于<Javascript高级程序设计>系列,我并没有弃坑, ...
- bzoj 3280: 小R的烦恼 费用流
题目: Description 小R最近遇上了大麻烦,他的程序设计挂科了.于是他只好找程设老师求情.善良的程设老师答应不挂他,但是要求小R帮助他一起解决一个难题. 问题是这样的,程设老师最近要进行一项 ...
- 【ML】关于神经网络优化问题的随笔记
1. 为什么不去试着最大化正确分类的图像数量而使用二次代价函数? 在神经网络中,被正确分类的图像数量所关于权重和偏置的函数并不是一个平滑的函数.大多数情况下,对权重和偏执做出的微小变动完全不会影响被正 ...
- CSS之EM相对单位
之前以为em单位只是在font-size中起到继承作用, 后来慢慢觉得,继承,应该是在几乎所有样式中都可以是实现的,比如height,width,border... 今天才简单测试了下,果真是可以实现 ...
- jquery给select赋值
项目中用到通过ajax请求数据然后给select赋值,由于经常遇到类似的代码,在这里把整个过程记录一下. 首选发出ajax请求如下: <script type="text/javasc ...