gpt4 book ai didi

java - 我无法使用 transaction.replace

转载 作者:行者123 更新时间:2023-12-02 02:17:11 24 4
gpt4 key购买 nike

我正在尝试用 fragment View 替换frame_layout的内容。我过去只使用 transaction.replace 来做到这一点,但它只是将 View 放在主要内容的顶部。

这是代码:

这就是我替换 ID 为 contains 的 frame_layout 的地方:

    VideosFragment videosFragment = new VideosFragment();
Bundle bundle = new Bundle();
bundle.putCharSequence("Character", view.getContentDescription());
videosFragment.setArguments(bundle);


FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.contain , videosFragment).addToBackStack(null).commit();

这是 main_Activity 的 xml

   <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.shobakyDC.muchannel.MainActivity">
<FrameLayout
android:id ="@+id/contain"
android:layout_width="match_parent"
android:background="@drawable/islamic_green"
android:layout_height="match_parent">


<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:paddingStart="50dp"
android:paddingTop="10dp"
android:verticalSpacing="10dp"
android:paddingRight="0dp"
android:paddingLeft="50dp"
android:paddingEnd="0dp">

</GridView>


</FrameLayout>

<android.support.design.widget.NavigationView
android:id="@+id/Navigator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/header"
android:layout_gravity="start"
android:background="@android:color/white"
app:itemTextColor="@color/navcolor"
app:itemIconTint="@color/navcolor"
app:menu="@menu/navigationmenu">
</android.support.design.widget.NavigationView>

视频 fragment :

    public class VideosFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.videofragment_layout , container , false);
Bundle args = getArguments();
TextView textView = view.findViewById(R.id.textView);
textView.setText(args.getString("Character"));
return view;
}
}

最佳答案

在 Main Activity 的 xml 中,您必须创建一个空的 FrameLayout,其 ID 如下所示:

<FrameLayout
android:id ="@+id/contain"
android:layout_width="match_parent"
android:background="@drawable/islamic_green"
android:layout_height="match_parent">
</FrameLayout>

然后为这个 fragment 制作一个单独的xml布局。现在您必须在主 Activity 中将此 fragment 布局替换为框架布局。

关于java - 我无法使用 transaction.replace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49152443/

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