一个小demo。用button+fragment实现导航栏切换界面,适合刚接触的新手看一下。
效果图
点击第二个后
源码:
主界面
<span style="font-size:18px;"><span style="font-size:14px;">package com.example.fragment; import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends FragmentActivity implements OnClickListener { Button b1, b2;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
ArticleFragment fragment;
ArticleFragment1 fragment1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager(); fragment = new ArticleFragment();
fragment1 = new ArticleFragment1();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fg, fragment);
fragmentTransaction.commit(); initView();
} private void initView() {
// TODO Auto-generated method stub
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(this);
b2.setOnClickListener(this); } @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.b1:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fg, fragment);
fragmentTransaction.commit();
break;
case R.id.b2:
FragmentTransaction fragmentTransaction1 = fragmentManager.beginTransaction();
fragmentTransaction1.replace(R.id.fg, fragment1);
fragmentTransaction1.commit();
break;
default:
break;
}
}
}
主界面布局:
<span style="font-size:18px;"><span style="font-size:14px;"><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="${relativePackage}.${activityClass}" > <LinearLayout
android:id="@+id/fg"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="1" > <Button
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:text="第一个" /> <Button
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:text="第二个" />
</LinearLayout> </RelativeLayout>
第一个fragment界面
<span style="font-size:18px;"><span style="font-size:14px;">package com.example.fragment; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class ArticleFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.article_view, container,false);
} }
第一个fragment的布局
<span style="font-size:18px;"><span style="font-size:14px;"><?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:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/homepage"/> </ScrollView> </LinearLayout>
第二个Fragment界面
<span style="font-size:18px;"><span style="font-size:14px;">package com.example.fragment; import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class ArticleFragment1 extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.article_view1, container,false);
} }
第二个fragment的布局文件
<span style="font-size:18px;"><span style="font-size:14px;"><?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:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/section"/> </ScrollView> </LinearLayout>
源码下载: