Android 3.0中引入fragment,主要为了解决不同屏幕分辩率的UI设计,使用fragment可以把屏幕划分成几块,这样便于管理,样式如微信。设计流程是建立四个fragment,根据监听事件响应,实现页面的切换,来两张效果图:
建立一个主布局与四个fragment布局
activity_main.xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_above="@+id/layout_chat"
android:background="#888" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/layout_chat"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/wechat" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="微信"
android:textColor="#888" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_addres"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/address_book" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="通讯录"
android:textColor="#888" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_find"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/find" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="发现"
android:textColor="#888" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_me"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="onClick" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/people" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="我"
android:textColor="#888" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
fragment的代码,随意添加什么都可以(如listview等),这里只加了TextView,其他三个一样
<?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="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂无聊天信息"
android:layout_marginLeft="80dp"
android:textColor="#778"
android:textSize="25sp"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
实现它需要FragmentManager对象与FragmentTransaction提交事务
public class MainActivity extends Activity {
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 声明fragment对象
fragmentManager = getFragmentManager();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.layout_chat:
ChioceItem(0);
break;
case R.id.layout_addres:
ChioceItem(1);
break;
case R.id.layout_find:
ChioceItem(2);
break;
case R.id.layout_me:
ChioceItem(3);
break;
default:
break;
}
}
public void ChioceItem(int index) {
switch (index) {
case 0:
fragmentTransaction = fragmentManager.beginTransaction();
fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.fragment, fragment1);
break;
case 1:
fragmentTransaction = fragmentManager.beginTransaction();
fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.fragment, fragment2);
break;
case 2:
fragmentTransaction = fragmentManager.beginTransaction();
fragment3 = new Fragment3();
fragmentTransaction.replace(R.id.fragment, fragment3);
break;
case 3:
fragmentTransaction = fragmentManager.beginTransaction();
fragment4 = new Fragment4();
fragmentTransaction.replace(R.id.fragment, fragment4);
break;
default:
break;
}
fragmentTransaction.commit();
}
}