Я пытаюсь создать Swappable fragements на моем андроид приложенияЗамена фрагментов по щелчку кнопки андроида
только половина части фрагмента кажется, заменив
скриншот:
xml файл для основного является
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity"
android:id="@+id/contentlayout">
<fragment
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/header_menufragment"
class="com.example.www.newapp.header_menu" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dynamicfragment"
class="com.example.www.newapp.mainfragment"/>
</LinearLayout>
всего dynamicfragment
предполагается заменить на projectfragment
нажатием кнопки «Проект».
project.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager()
.beginTransaction()
.replace(R.id.dynamicfragment, new projectfragment())
.addToBackStack(null)
.commit();
}
});
projectfragment
класс имеет
public class projectfragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_project, container,false);
}
}
файл макета
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="@color/colorDarkRed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight=".5">
<ImageButton
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton
android:id="@+id/img2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<ImageButton
android:id="@+id/img3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/imgfile"/>
</LinearLayout>
Пожалуйста, помогите мне решить эту проблему.
Это не совсем ясно, что он заменяет половину фрагмента, вы можете поделиться макеты двух фрагментов? – himanshu1496
yes Я обновил файл, который вы задали –
Насколько мне известно, нельзя заменить фрагмент, определенный статически в файле макета. удивлен тем, что видел этот результат .. !!!! –