采用fragment进行页面切换,仿照《零基础学安卓》5.4.3-实例-04写了一个仿微信底边切换。
修改前源码:
下面是main_activity.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.studenttimemanage.MainActivity">
<!--Fragment组件-->
<fragment
android:id="@+id/fragment"
android:name="com.example.studenttimemanage.fragment.TimeRecord"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<!--微信图标-->
<ImageView
android:id="@+id/TimeRecord"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_1" />
<!--通讯录图标-->
<ImageView
android:id="@+id/TypeManage"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_2" />
<!--发现图标-->
<ImageView
android:id="@+id/image3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_3" />
<!--我图标-->
<ImageView
android:id="@+id/image4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:src="@drawable/bottom_4" />
</LinearLayout>
</RelativeLayout>
暂时只修改了前面两个页面,更改了图片view名字,在后面进行引用。
MainActivity.java如下:
package com.example.studenttimemanage;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.example.studenttimemanage.fragment.TimeRecord;
import com.example.studenttimemanage.fragment.TypeManage;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取布局文件的第一个导航图片
ImageView imageView1 = (ImageView) findViewById(R.id.TimeRecord);
//获取布局文件的第二个导航图片
ImageView imageView2 = (ImageView) findViewById(R.id.TypeManage);
//获取布局文件的第三个导航图片
ImageView imageView3 = (ImageView) findViewById(R.id.image3);
//获取布局文件的第四个导航图片
ImageView imageView4 = (ImageView) 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 v) {
FragmentManager fm = getFragmentManager(); // 获取Fragment
FragmentTransaction ft = fm.beginTransaction(); // 开启一个事务
Fragment f = null; //为Fragment初始化
switch (v.getId()) { //通过获取点击的id判断点击了哪个张图片
case R.id.TimeRecord:
f = new TimeRecord(); //创建第一个Fragment
break;
case R.id.TypeManage:
f = new TypeManage() ;
break;
default:
break;
}
ft.replace(R.id.fragment, f); //替换Fragment
ft.commit(); //提交事务
}
};
}
使用了replace的方法,主要是不太会如何修改成add()和hide(),尝试过之后还是放弃了。
出现的问题:
点击下边图片view切换时,初始界面不变,新界面出现在初始界面下方。
没有发生重叠现象,并且下面fragment切换正常。
问题解决方法(不完善):
在网上找了很多,大多是解决重叠问题的。
推测这个问题并不像是fragment切换产生的,更像是初始界面没有正确消失的问题,不知道是不是哪里的代码有错误。
最后发现了一个方法
将avtivity_main.xml中的fragment
<fragment
android:id="@+id/fragment"
android:name="com.example.studenttimemanage.fragment.TimeRecord"
android:layout_width="match_parent"
android:layout_height="match_parent" />
修改为FrameLayout
<FrameLayout
android:id="@+id/fragment"
android:name="com.example.studenttimemanage.fragment.TimeRecord"
android:layout_width="match_parent"
android:layout_height="match_parent" />
页面上下叠加问题确实消失了,但是没有了初始页面。
(对毕设来说凑合能用,等做完毕设回来再研究一下)