最近在使用Fragment切换操作时,遇到2个fragment有重叠部分,经过仔细分析发现,是另外一个(RightFragment)布局下面textview的问题,在做2个fragment切换的时候总有一部分是重叠的,原因是出在textview是占据RightFragment的一部分布局,可以把布局里面textview的android:visibility=gone;把textview先隐藏起来,然后在需要显示的地方将其
setVisibility(View.VISIBLE)其他不需要的地方设置为setVisibility(View.GONE)即可
=================>>>>>正常切换==================>>>>
============================>因为Textview控件造成的重叠如图::
RigthFragment布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0059"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvReceiveInfo"
android:textSize="30dp"
android:textColor="#f1139303"
android:visibility="gone" //将其隐藏起来,此处可以省略,但是需要在需要隐藏的代码处将其隐藏
/>
</LinearLayout>
RightFragment将其显示出来
package fragment.tian.io.androidfrg;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.Fragment;
public class RightFragment extends Fragment {
private TextView tvReceiveInfo;
public RightFragment() {
// Required empty public constructor
}
private String str1;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle=getArguments();
if(bundle!=null) {
str1 = bundle.getString("strData");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_right, container, false);
tvReceiveInfo=(TextView)view.findViewById(R.id.tvReceiveInfo);
// if (!str1.equals(""))
// {
// tvReceiveInfo.setText(str1);
// }
return view;
}
public void updateTextView(String str)
{
tvReceiveInfo.setVisibility(View.VISIBLE); //将其显示出来
tvReceiveInfo.setText(str);
}
}
这样就以实现带有textview空间的fragment重叠问题~