标签:
做一个简单的点击左右切换页面,下次再发一个点击后底部图标也会切换和文字也会变换颜色的
只是发教程,所以就用eclipse写,没用AS

代码如下
MainActivity.java
package cn.wuxiaocheng.fragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private FragmentManager fm;
private FragmentTransaction ft;
//两个页面
private FragmentHome fh;
private FragmentSet fs;
//两个底部
private TextView tvhome;
private TextView tvset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉顶部标题栏
setContentView(R.layout.activity_main);
//初始化底部
initView();
//设置默认的Fragment
setDefaultFragment();
}
//设置默认的Fragment
private void setDefaultFragment() {
fm = getFragmentManager();
ft = fm.beginTransaction();
fh = new FragmentHome();
ft.replace(R.id.main_view, fh);
ft.commit();
}
//初始化空间和声明事件
private void initView() {
tvhome = (TextView) findViewById(R.id.id_home);
tvset = (TextView) findViewById(R.id.id_set);
tvhome.setOnClickListener(this);
tvset.setOnClickListener(this);
}
//点击事件
@Override
public void onClick(View v) {
fm = getFragmentManager();
//开启fragment事物
ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.id_home:
if (fh == null) {
fh = new FragmentHome();
}
ft.replace(R.id.main_view, fh);
break;
case R.id.id_set:
if (fs == null) {
fs = new FragmentSet();
}
ft.replace(R.id.main_view, fs);
break;
}
// ft.addToBackStack();
//事物提交
ft.commit();
}
}
FragmentHome.java
package cn.wuxiaocheng.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentHome extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home,null);
return view;
}
}
FragmentSet.java
package cn.wuxiaocheng.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentSet extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_set,null);
return view;
}
}
布局代码如下:
activity_main.xml
<LinearLayout 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:orientation="vertical" > <FrameLayout android:id="@+id/main_view" android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="1"></FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:background="#eeeeee" > <TextView android:id="@+id/id_home" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="首页" /> <TextView android:id="@+id/id_set" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="设置" /> </LinearLayout> </LinearLayout>
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="26sp" android:text="Home"/> </LinearLayout>
fragment_set.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00FF66" android:gravity="center" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textColor="#FF0033" android:textSize="26sp" android:text="Set"/> </LinearLayout>
效果如下图

标签:
原文地址:http://my.oschina.net/u/2264427/blog/477898