- 完成上述的网页,并且能够左右切换
- 注:
- 一下的带代码只能够在Android的3.0以上的版本中使用。
- 这是java文件的源代码
- /*
- *作者浅淡~夏天
- *Time:2014/12/14
- *微信界面
- */
- package com.android.xiong.fragmentnavigation2;
- import android.app.Activity;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.RadioButton;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private RadioButton ra_weixin_bt, ra_tongxunlu_bt, ra_faxian_bt, ra_wo_bt;
- private Fragment talkFragment, addressFragment, findFragment, meFragment;
- FragmentManager fgManager;
- private int i = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- fgManager = getFragmentManager();
- init();
- ra_weixin_bt.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- talkFragment = new WeChatTalkFragment();
- changeFrament(talkFragment, null, "talkFragment");
- changeRadioButtonImage(v.getId());
- }
- });
- ra_tongxunlu_bt.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- addressFragment = new AddressFragment();
- changeFrament(addressFragment, null, "addressFragment");
- changeRadioButtonImage(v.getId());
- }
- });
- ra_faxian_bt.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- findFragment = new FindFragment();
- changeFrament(findFragment, null, "findFragment");
- changeRadioButtonImage(v.getId());
- }
- });
- ra_wo_bt.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- meFragment = new MeFragment();
- changeFrament(meFragment, null, "meFragment");
- changeRadioButtonImage(v.getId());
- }
- });
- }
- // 初始化信息
- public void init() {
- ra_weixin_bt = (RadioButton) findViewById(R.id.ra_weixin_bt);
- ra_tongxunlu_bt = (RadioButton) findViewById(R.id.ra_tongxunlu_bt);
- ra_faxian_bt = (RadioButton) findViewById(R.id.ra_faxian_bt);
- ra_wo_bt = (RadioButton) findViewById(R.id.ra_wo_bt);
- talkFragment = new WeChatTalkFragment();
- addressFragment = new AddressFragment();
- findFragment = new FindFragment();
- meFragment = new MeFragment();
- changeFrament(talkFragment, null, "talkFragment");
- // 更换 android:drawableTop图pain资源
- ra_weixin_bt.setCompoundDrawablesWithIntrinsicBounds(0,
- R.drawable.n_talk_l, 0, 0);
- }
- // 切界面
- public void changeFrament(Fragment fragment, Bundle bundle, String tag) {
- for (int i = 0, count = fgManager.getBackStackEntryCount(); i < count; i++) {
- fgManager.popBackStack();
- }
- FragmentTransaction fg = fgManager.beginTransaction();
- fragment.setArguments(bundle);
- fg.add(R.id.fragmentRoot, fragment, tag);
- fg.addToBackStack(tag);
- fg.commit();
- }
- // 更换RadioButton图片
- public void changeRadioButtonImage(int btids) {
- int[] imageh = { R.drawable.n_address_h, R.drawable.n_find_h,
- R.drawable.n_me_h, R.drawable.n_talk_h };
- int[] imagel = { R.drawable.n_address_l, R.drawable.n_find_l,
- R.drawable.n_me_l, R.drawable.n_talk_l };
- int[] rabt = { R.id.ra_tongxunlu_bt, R.id.ra_faxian_bt, R.id.ra_wo_bt,
- R.id.ra_weixin_bt };
- switch (btids) {
- case R.id.ra_tongxunlu_bt:
- changeImage(imageh, imagel, rabt, 0);
- break;
- case R.id.ra_faxian_bt:
- changeImage(imageh, imagel, rabt, 1);
- break;
- case R.id.ra_wo_bt:
- changeImage(imageh, imagel, rabt, 2);
- break;
- case R.id.ra_weixin_bt:
- changeImage(imageh, imagel, rabt, 3);
- break;
- default:
- break;
- }
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- if (i == 0) {
- Toast.makeText(this, "再点击一次将退出程序", Toast.LENGTH_SHORT).show();
- i++;
- } else {
- this.finish();
- }
- return false;
- }
- return super.onKeyDown(keyCode, event);
- }
- public void changeImage(int[] image1, int[] image2, int[] rabtid, int index) {
- for (int i = 0; i < image1.length; i++) {
- if (i != index) {
- ((RadioButton) findViewById(rabtid[i]))
- .setCompoundDrawablesWithIntrinsicBounds(0, image1[i],
- 0, 0);
- } else {
- ((RadioButton) findViewById(rabtid[i]))
- .setCompoundDrawablesWithIntrinsicBounds(0, image2[i],
- 0, 0);
- }
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
- 一下是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"
- android:background="#dbdbdb"
- tools:context=".MainActivity" >
- <LinearLayout
- android:id="@+id/fragmentRoot"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- </LinearLayout>
- <LinearLayout
- android:id="@+id/bottomList"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:background="@android:color/black"
- android:orientation="horizontal" >
- <LinearLayout style="@style/linaer_bottom" >
- <RadioButton
- android:id="@+id/ra_weixin_bt"
- style="@style/rbt_bottom"
- android:drawableTop="@drawable/n_talk_h"
- android:text="@string/weixin" />
- </LinearLayout>
- <LinearLayout style="@style/linaer_bottom" >
- <RadioButton
- android:id="@+id/ra_tongxunlu_bt"
- style="@style/rbt_bottom"
- android:drawableTop="@drawable/n_address_h"
- android:text="@string/tongxulu" />
- </LinearLayout>
- <LinearLayout style="@style/linaer_bottom" >
- <RadioButton
- android:id="@+id/ra_faxian_bt"
- style="@style/rbt_bottom"
- android:drawableTop="@drawable/n_find_h"
- android:text="@string/faxian" />
- </LinearLayout>
- <LinearLayout style="@style/linaer_bottom" >
- <RadioButton
- android:id="@+id/ra_wo_bt"
- style="@style/rbt_bottom"
- android:drawableTop="@drawable/n_me_h"
- android:text="@string/wo" />
- </LinearLayout>
- </LinearLayout>
- </RelativeLayout>
时间: 2024-12-28 12:36:48