您的位置:首页 > 其它

利用fragment+radiogroup实现底部标题栏

2015-12-28 10:09 1361 查看
最近做项目时因为要实现防微信的底部导航栏功能,实现的方法挺多的,网上可以找到例子与源码,在这里我只写出一种我用的比较好的方式,就是用fragment+radiogroup来实现底部导航栏的功能,代码中有相关注释,而且我会上传源代码(这源码是网上下载的 嘻嘻QAQ),因此我就把布局文件与java代码贴出来,供大家无聊时看看

主布局代码 activity_main

<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"

tools:context="com.example.fragment.MainActivity" >

<FrameLayout

android:id="@+id/frame_container"

android:layout_height="match_parent"

android:layout_width="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"

android:layout_marginBottom="2dip"

android:background="#ffc9cacb" />

<RadioGroup

android:id="@+id/main_radiogroup"

android:layout_height="wrap_content"

android:layout_width="match_parent"

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>

接下来是mainactivity

public class MainActivity extends Activity {

private RadioGroup radiogroup;

private FragmentManager fm;

private FragmentTransaction ft;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

radiogroup=(RadioGroup) findViewById(R.id.main_radiogroup);

fm=getFragmentManager();

radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

/** 改变文字的颜色*/

int length = group.getChildCount();

for (int i = 0; i < length; i++) {

RadioButton rgo=(RadioButton) group.getChildAt(i);

if(rgo.getId() != checkedId)

rgo.setTextColor(getResources().getColor(R.color.tv_checked_nor));

else

rgo.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;

}

}

});

}

private void changeQuesion() {

ft = fm.beginTransaction();

TabFragment tab1 = new TabFragment();

Bundle bundle = new Bundle();

bundle.putString("name", "问他");

tab1.setArguments(bundle);

ft.replace(R.id.frame_container, tab1);

ft.commit();

}

/**

* 切换消息

*/

private void changeMessage() {

ft = fm.beginTransaction();

TabFragment tab1 = new TabFragment();

Bundle bundle = new Bundle();

bundle.putString("name", "消息");

tab1.setArguments(bundle);

ft.replace(R.id.frame_container, tab1);

ft.commit();

}

/***

* 切换登录

*/

private void changeUser(){

ft=fm.beginTransaction();

TabFragment tab1 = new TabFragment();

Bundle bundle = new Bundle();

bundle.putString("name", "用户");

tab1.setArguments(bundle);

ft.replace(R.id.frame_container, tab1);

ft.commit();

}

/***

* 切换回答

*/

private void changeAnswer(){

ft=fm.beginTransaction();

TabFragment tab1 = new TabFragment();

Bundle bundle = new Bundle();

bundle.putString("name", "回答");

tab1.setArguments(bundle);

ft.replace(R.id.frame_container, tab1);

ft.commit();

}

/***

* 切换回答

*/

private void changeDiscover(){

ft=fm.beginTransaction();

TabFragment tab1 = new TabFragment();

Bundle bundle = new Bundle();

bundle.putString("name", "发现");

tab1.setArguments(bundle);

ft.replace(R.id.frame_container, tab1);

ft.commit();

}

}

上面也就是fragment的使用方法而已,其中TabFragment1就是显示内容的fragment,可以根据要求自己添加。现在上传源码吧
http://download.csdn.net/detail/caohuicong/9388524
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: