java - fragment 布局问题

标签 java android android-fragments

当我将数据从activity传递到fragment时,我得到这个:

/image/L0Si4.png

为什么 fragment 会重叠?我只需要选中复选框的 fragment 。

这是我传递数据时的代码:

Bundle bundle = new Bundle();
            bundle.putIntegerArrayList("oki", hm);
            bundle.putIntegerArrayList("okiquantitapizze", hm_quantitàpizze);

            System.out.println("PERO:" + bundle);
 MyListFragment2 myFragment = MyListFragment2.newInstance(hm,hm_quantitàpizze);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(android.R.id.content, myFragment);
            transaction.commit();

我的列表 fragment 2:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        System.out.println("BUNDLES1 prima:" + bundle);

        bundle=getArguments();

        System.out.println("BUNDLES1 dopo:" + bundle);



        if (bundle != null) {
            strtext2 = bundle.getIntegerArrayList("daje");
            System.out.println("BUNDLES1 prima:" + strtext2);

            quantitàpizze2 = bundle.getIntegerArrayList("daje2");
            System.out.println("BUNDLES1 prima:" + quantitàpizze2);

        }

}




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the custom_spinner_items for this fragment
        //super.onDestroy();


        SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();


        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list_2_2, container, false);





        return rootView;


    }




    public static MyListFragment2 newInstance(ArrayList<Integer> hm, ArrayList<Integer> hm_quantitàpizze) {
        MyListFragment2 fragment = new MyListFragment2();
        Bundle args = new Bundle();
        args.putIntegerArrayList("daje", hm);
        args.putIntegerArrayList("daje2", hm_quantitàpizze);
        fragment.setArguments(args);
        return fragment;
    }

FRAGMENT_LIST_2_2:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/acab"
    android:layout_marginLeft="5dp"


    >
    <!--android:background="@color/white"-->
    <ListView
        android:id="@+id/listvieww"
        android:layout_marginTop="120dp"
        android:layout_width="fill_parent"
        android:layout_height="match_parent">

    </ListView>




</FrameLayout>

ListView 的项目:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimaryDark"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:gravity="center"
                android:orientation="horizontal" >


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/welcome"
                    android:textSize="20dp"
                    android:layout_marginLeft="5dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="20dp"
                    android:layout_marginLeft="5dp"
                    android:id="@+id/numero_tavolo"
                    />

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:textColor="@color/lbl_name"
                    android:textSize="24dp"
                    android:layout_marginLeft="5dp"
                    />
              <!--  <Button
                    android:id="@+id/btnLogout"
                    android:layout_width="122dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="40dip"
                    android:background="@color/btn_login"
                    android:text="@string/btn_logout"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    android:textSize="15dp"
                    android:layout_marginLeft="5dp"
                    android:onClick="logoutUser"

                    />-->

            </LinearLayout>

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabss"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            android:background="@color/colorPrimaryDark" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpagerr"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >
     <!--   <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
       <FrameLayout
           android:id="@+id/pero"

        android:layout_width="match_parent"
            android:layout_height="match_parent" />
        </LinearLayout>-->

    </android.support.v4.view.ViewPager>


</android.support.design.widget.CoordinatorLayout>

如果我放

android:background="@color/white"为:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/acab"
    android:layout_marginLeft="5dp"
    android:background="@color/white"

    >
    <ListView
        android:id="@+id/listvieww"
        android:layout_marginTop="120dp"
        android:layout_width="fill_parent"
        android:layout_height="match_parent">

    </ListView>




</FrameLayout>

我明白了:

enter image description here

最佳答案

你应该在 fragment 背景中使用一些颜色

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/acab"
    android:layout_marginLeft="5dp"
    android:background="@color/white">

    <ListView
        android:id="@+id/listvieww"
        android:layout_marginTop="120dp"
        android:layout_width="fill_parent"
        android:layout_height="match_parent">

    </ListView>




</FrameLayout>

我可以在这张图片中看到它没有重叠。

enter image description here

关于java - fragment 布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45892751/

相关文章:

android - getTargetFragment 返回 Null

java - 片段无法工作并且有错误

java - ImageIO.read 正在终止程序,没有错误

java - keyPressed 监听器产生奇怪的行为

java - thread.sleep(5000) 不起作用

android - 如何为不同的 Activity 实现对话?

android - 如何在 Android Studio 中清除最近的项目列表?

java - 未处理的异常 : PlatformException (sign_in_failed, com.google.android.gms.common.api.ApiException : 10: , null, null) - 在一个系统中运行良好

java - 将所有输入转换为大写

android - 如何在MainActivity中保存在不同事务中提交的两个 fragment 的InstanceState?