我尝试在单击按钮时将fragment
1替换为Fragment
2,但片段没有被替换,它位于顶部。
两个片段的内容都保留在屏幕上,我做错了什么?我能做什么?
enter image description here
这是片段1的类别
public class Login extends Fragment {
Button btn_login;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public Login() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Login.
*/
// TODO: Rename and change types and number of parameters
public static Login newInstance(String param1, String param2) {
Login fragment = new Login();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista = inflater.inflate(R.layout.fragment_login, container, false);
btn_login = vista.findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create new fragment and transaction
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setReorderingAllowed(true);
// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.layout_login,fragment_menu_seleccion.newInstance("",""));
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
});
return vista;
}
这是片段2的类别
public class fragment_menu_seleccion extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public fragment_menu_seleccion() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment fragment_menu_seleccion.
*/
// TODO: Rename and change types and number of parameters
public static fragment_menu_seleccion newInstance(String param1, String param2) {
fragment_menu_seleccion fragment = new fragment_menu_seleccion();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_menu_seleccion, container, false);
}}
片段1的xml
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Controlador.Login"
>
<!-- TODO: Update blank fragment layout -->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#219EBC"
>
<TextView
android:id="@+id/txtTitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:text="@string/txtBinvenidaLogin"
android:textSize="20sp"
android:textColor="#B3ffffff"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:text="@string/txtBinvenidaLogin2"
android:textColor="#ffffff"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle1" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
android:background="@drawable/cardbox"
android:paddingLeft="20dp"
android:paddingRight="20dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="-39dp">
<LinearLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="274dp"
android:layout_height="116dp"
android:contentDescription="@string/dscriptIMGlogin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/heuristictool1" />
<TextView
android:id="@+id/titleLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_mono_medium"
android:text="@string/TextoMobile"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</LinearLayout>
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="User"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#000000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="67dp"
android:layout_marginBottom="30dp"
android:onClick="Login"
android:text="@string/btnLogin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
片段2的xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment_menu_seleccion">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This a Fragment 2" />
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="@+id/grupoFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-8dp"
app:fab_addButtonColorNormal="@color/Ui_colorContraste"
>
<com.getbase.floatingactionbutton.AddFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_crearEV"
app:fab_addButtonColorNormal="@color/Ui_colorContraste"
app:fab_icon="@drawable/ic_icon_lapiz"
/>
</com.getbase.floatingactionbutton.FloatingActionsMenu>
</RelativeLayout>
</FrameLayout>
1条答案
按热度按时间6ioyuze21#
要隐藏片段中下面的内容,您必须给予它一个类似白色的背景。因此,在您的布局文件中,在根布局中,将其背景设置为白色。
或