碎片一个接一个

Fragments goes one behind the other

我制作了一个 2 片段布局,一个用于登录屏幕,另一个用于注册屏幕,在主屏幕中,我放置了一个用于登录屏幕的片段和一个按钮,如果用户想要注册,他可以单击该按钮,然后注册片段出现在他面前,登录片段消失 问题是在我按下主屏幕中的注册按钮后,登录屏幕似乎在注册屏幕后面 这是它的照片 login behind Register 我不知道有什么问题或者我误解了我不知道的片段 这是 MainActivity 上的 onclick 方法 class

  @Override
                        public void onClick(View view) {
                            String mytxt = inversebutton.getText().toString();
                            Fragment fragmentSign = new Signin();
                            Fragment fragmentregister = new Register();
                            if(mytxt.equals("Register")){  // call signin frame
                                android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
                                fragmentManager.beginTransaction().replace(R.id.fragmentplace ,fragmentregister).commit();
                                inversebutton.setText("SignIn");
                            }else{ // call register fra
                                android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
                                fragmentManager.beginTransaction().replace(R.id.fragmentplace ,fragmentSign).commit();
                                inversebutton.setText("Register");
                            }
                        }
                    });

这是activitymain.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.abdelmagied.myapplication.MainActivity"
        android:background="#222">

        <fragment
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:name="com.example.abdelmagied.myapplication.Signin"
            android:id="@+id/fragmentplace"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:text="Register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/reverse"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="16dp" />

    </RelativeLayout>

这里是register_fragmentclass

  public class Register extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_register, container, false);
}

这是登录片段class

public class Register extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_register, container, false);
}

这是fragment_register.xml

<RelativeLayout 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.example.abdelmagied.myapplication.Register"
        android:background="#090"
        android:id="@+id/registerfragment">

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

        <TextView
            android:text="Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="32dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="49dp"
            android:id="@+id/textView" />

        <TextView
            android:text="Password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_alignLeft="@+id/textView"
            android:layout_alignStart="@+id/textView"
            android:layout_marginTop="61dp"
            android:id="@+id/textView3" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:layout_alignParentTop="true"
            android:layout_alignLeft="@+id/Rbutton"
            android:layout_alignStart="@+id/Rbutton"
            android:layout_marginTop="30dp"
            android:id="@+id/registerN" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:layout_below="@+id/registerN"
            android:layout_alignRight="@+id/registerN"
            android:layout_alignEnd="@+id/registerN"
            android:layout_marginTop="36dp"
            android:id="@+id/Rpassword" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/RRpassword"
            android:layout_alignBaseline="@+id/textView4"
            android:layout_alignBottom="@+id/textView4"
            android:layout_alignLeft="@+id/Rbutton"
            android:layout_alignStart="@+id/Rbutton" />

        <Button
            android:text="Register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Rbutton"
            android:layout_below="@+id/textView4"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="57dp" />

        <TextView
            android:text="Rpassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView4"
            android:layout_centerVertical="true"
            android:layout_alignLeft="@+id/textView3"
            android:layout_alignStart="@+id/textView3" />
    </RelativeLayout>

这是fragment_signin.xml

    <RelativeLayout 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.example.abdelmagied.myapplication.Signin"
        android:background="#878"
        android:id="@+id/signinfragment">

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

        <TextView
            android:text="Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="55dp"
            android:layout_marginStart="55dp"
            android:id="@+id/sn"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="46dp" />

        <TextView
            android:text="Password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sn"
            android:layout_alignLeft="@+id/sn"
            android:layout_alignStart="@+id/sn"
            android:layout_marginTop="79dp"
            android:id="@+id/textView2" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:layout_above="@+id/textView2"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/sname" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:layout_alignBottom="@+id/textView2"
            android:layout_alignLeft="@+id/sname"
            android:layout_alignStart="@+id/sname"
            android:id="@+id/signpassword" />

        <Button
            android:text="SignIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Sbuton"
            android:layout_below="@+id/signpassword"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="51dp" />
    </RelativeLayout>

我认为您应该使用 DialogFragment 此处用于在其他人面前弹出的那种弹出窗口。

你就这么用

DialogFragment registerFragment = RegisterFragment.newInstance(0);
registerFragment.show(getFragmentManager().beginTransaction(), "RegisterPopup");

您的片段将属于 class DialogFragment。通常你弹出一个 Dialog 开始。然后,根据您的需要,可以有多种选择。

public class RegisterFragment extends DialogFragment {
    ...        

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ...
    }
}

您可以在 Android Developer 上找到示例 DialogFragment

与其在 xml 中获取片段标签,不如通过提供其 id 并通过片段事务动态替换它来获取布局

在您的布局中,通过采用简单的相对或线性布局来更改它,并且必须为该布局提供背景,删除该片段标签并根据您的需要通过 java 编码替换片段

然后在 onCreate() 方法中 class 的开头将其替换为 ->`

getSupportFragmentManager().beginTransaction().replace(R.id.fragmentplace, new Your_frag(), Constant.FragmentTags.fragmentTag).commit();