使用片段事务重叠布局 android
Overlapping layouts using fragment transaction android
所以我正在尝试从主 activity 加载我的登录片段。我想我在使用片段转换 replace() 方法时无法替换整个布局时遇到问题。
这是我的 activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
}
@Override
protected void onStart() {
super.onStart();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
LoginFragment lf = new LoginFragment();
ft.replace(R.id.mapView, lf);
ft.addToBackStack("map to login");
ft.commit();
}
activity_maps.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:showIn="@layout/activity_maps">
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/activity_maps" />
<Button
android:id="@+id/btn
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
login_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".LoginActivity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:inputType="textPersonName"
android:text="@string/Username"
android:ems="10"
android:id="@+id/username"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:inputType="textPersonName"
android:text="@string/Password"
android:ems="10"
android:id="@+id/password"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/ForgotPassword"
android:id="@+id/forgotPassword"
android:layout_gravity="center_horizontal"
android:textColor="#197cff" />
<Button
android:id="@+id/GoogleButton"
android:layout_width="215dp"
android:layout_height="wrap_content" />
</LinearLayout>
关于我应该如何重组我的 xml 文件的任何建议?
感谢您的宝贵时间!
Any recommendations on how I should restructure my xml files?
您的问题与xml布局无关。首先将 MapFragment 嵌入 activity 布局中,然后将其替换为另一个片段作为 activity starts(onStart()) 根本没有任何意义。其次,嵌入式片段(意思是在 xml 中用片段标签声明的片段)不能用于片段交易,它们是静态的。因此,从 xml 布局中删除 MapFragment,并执行一个简单的事务,将 LoginFragment 添加到 activity 布局中的 empty ViewGroup 中。
所以我正在尝试从主 activity 加载我的登录片段。我想我在使用片段转换 replace() 方法时无法替换整个布局时遇到问题。
这是我的 activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
}
@Override
protected void onStart() {
super.onStart();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
LoginFragment lf = new LoginFragment();
ft.replace(R.id.mapView, lf);
ft.addToBackStack("map to login");
ft.commit();
}
activity_maps.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:showIn="@layout/activity_maps">
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/activity_maps" />
<Button
android:id="@+id/btn
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
login_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".LoginActivity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:inputType="textPersonName"
android:text="@string/Username"
android:ems="10"
android:id="@+id/username"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:inputType="textPersonName"
android:text="@string/Password"
android:ems="10"
android:id="@+id/password"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/ForgotPassword"
android:id="@+id/forgotPassword"
android:layout_gravity="center_horizontal"
android:textColor="#197cff" />
<Button
android:id="@+id/GoogleButton"
android:layout_width="215dp"
android:layout_height="wrap_content" />
</LinearLayout>
关于我应该如何重组我的 xml 文件的任何建议?
感谢您的宝贵时间!
Any recommendations on how I should restructure my xml files?
您的问题与xml布局无关。首先将 MapFragment 嵌入 activity 布局中,然后将其替换为另一个片段作为 activity starts(onStart()) 根本没有任何意义。其次,嵌入式片段(意思是在 xml 中用片段标签声明的片段)不能用于片段交易,它们是静态的。因此,从 xml 布局中删除 MapFragment,并执行一个简单的事务,将 LoginFragment 添加到 activity 布局中的 empty ViewGroup 中。