gpt4 book ai didi

java - 替换基本 fragment 及其布局时 fragment 重叠

转载 作者:搜寻专家 更新时间:2023-11-01 08:47:04 24 4
gpt4 key购买 nike

我会直奔问题。在 Android 中替换 fragment 时我做错了什么?单击赞助商时的预期结果:

enter image description here

现实(我实际得到的:基本上,它只是赞助商 fragment ,它只是放在其他 fragment 上):

enter image description here

我正在调用 fragment

fragment = new Other(this);

赞助商.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Sponsors" />

</RelativeLayout>

赞助商.java

    ...
public class Sponsors extends android.support.v4.app.Fragment {
private Context context;

public Sponsors(Context context) {
this.context = context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.sponsors, null);
return rootView;
}
}

其他.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/other_frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="COMMON"
android:textStyle="bold"
android:textColor="@color/header_red"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@color/header_red"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/sponsors"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sponsors"
android:textColor="@color/subtext"
android:textSize="16sp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="15dp" />
</LinearLayout>
</FrameLayout>

其他.java

public class Other extends android.support.v4.app.Fragment {
public Other(Context context) {
this.context = context;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.other, container, false);
sponsors = (TextView) rootView.findViewById(R.id.sponsors);
sponsors.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (savedInstanceState == null) {
Sponsors duedateFrag = new Sponsors();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.other_frame_container, duedateFrag);
ft.addToBackStack(null);
ft.commit();
}
}
});
}

最佳答案

FragmentTransaction中的add改为replace

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.other_frame_container, duedateFrag);

关于java - 替换基本 fragment 及其布局时 fragment 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27216099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com