Android Studio 切换fragment重叠问题

本文介绍了一种解决两个Fragment切换时出现重叠现象的方法。通过调整TextView的可见性,在需要时显示,在不需要时隐藏,有效避免了重叠问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在使用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重叠问题~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值