各fragment的xml及類文件不再一一給出,java文件簡單地佈局文件的onCreateView();方法綁定各自xml文件並返回一個View。
主佈局Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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.wechat_interface_change.MainActivity">
<fragment
android:id="@+id/fragment"
**android:name="com.example.wechat_interface_change.Wechat_Fragment"** //默認綁定wechat_fragment
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
>
<!--微信圖標-->
<ImageView
android:id="@+id/image1"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1"
android:src="@drawable/bottom_1"
/>
<!--通訊錄圖標-->
<ImageView
android:id="@+id/image2"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1"
android:src="@drawable/bottom_2"
/>
<!--發現圖標-->
<ImageView
android:id="@+id/image3"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1"
android:src="@drawable/bottom_3"
/>
<!--我圖標-->
<ImageView
android:id="@+id/image4"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1"
android:src="@drawable/bottom_4"
/>
</LinearLayout>
</RelativeLayout>
主程序MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取ImageView
ImageView imageView1=findViewById(R.id.image1);
ImageView imageView2=findViewById(R.id.image2);
ImageView imageView3=findViewById(R.id.image3);
ImageView imageView4=findViewById(R.id.image4);
//設置監聽器
imageView1.setOnClickListener(l);
imageView2.setOnClickListener(l);
imageView3.setOnClickListener(l);
imageView4.setOnClickListener(l);
}
//定義監聽器
View.OnClickListener l=new View.OnClickListener(){
@Override
public void onClick(View view) { //點擊方法
FragmentManager fm =getFragmentManager(); //實例化Fragment處理器
FragmentTransaction ft=fm.beginTransaction(); //創建Fragment事務
Fragment f=null; //初始化f
switch(view.getId()){ //判斷單擊哪張圖片
case R.id.image1:
f=new Wechat_Fragment(); //創建第一個Fragment
break;
case R.id.image2:
f=new tongxunlu_fragment(); //創建第二個Fragment
break;
case R.id.image3:
f=new faxian_fragment(); //創建第三個Fragment
break;
case R.id.image4:
f=new wo_fragment(); //創建第四個Fragment
break;
default:
break;
}
ft.replace(R.id.fragment,f); //用f替換Fragment
ft.commit(); //提交事務
}
};
概念圖:
效果圖: