首页 > 解决方案 > 使用片段

问题描述

无法在设备上正确显示片段。我有一个活动框架和一个片段框架。尝试用 Fragment 中的框架替换活动框架。但是我得到的是活动中的框架被混合在一起,下面的片段框架显示。

这是调用片段的代码。

    public void setupMultiItems(String mScan){
    Timber.d("Multiple matching Items");

    Bundle bundle = new Bundle();
    bundle.putString("valuesArray",mScan);
    SelectItemFragment fragobj = new SelectItemFragment();
    fragobj.setArguments(bundle);
    FragmentManager fragmentManager=getFragmentManager();
    FragmentTransaction fragmentTransaction  = fragmentManager.beginTransaction();
   fragmentTransaction.replace(R.id.FragmentFrame,  fragobj);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

这是活动的 XML。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackgroundGray"
    tools:layout="@layout/fragment_select_item"
    tools:context="com.procatdt.stockright.activities.PutAwayAreaActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"

       >

        <include
            android:id="@+id/include2"
            layout="@layout/frm1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_gravity="left"
            android:layout_weight="1" />

        <FrameLayout
            android:id="@+id/FragmentFrame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

        </FrameLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:paddingLeft="5dp"
        android:orientation="vertical">

        <include
            android:id="@+id/include"
            layout="@layout/layout_numpad5"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="220dp"
            android:layout_weight="1"
            android:layout_gravity="right"
           />

    </LinearLayout>

</RelativeLayout>

这是片段的 XML。

    <FrameLayout 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.procatdt.stockright.activities.SelectItemFragment">

    <!-- TODO: Update blank fragment layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:id="@+id/newFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true">

            <Button
                android:id="@+id/btnSelectItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Select Item" />
        </FrameLayout>
    </RelativeLayout>
</FrameLayout>

活动的所有控件都被缩小并与下面显示的片段中的按钮一起卡住。

关于我所缺少的任何帮助。在过去的 18 年里,我的背景一直是 VB。这是我的第一个 Android Studio Java 项目,一直被碎片所困扰。

标签: javaandroidandroid-fragments

解决方案


推荐阅读