android片段用框架布局替换fiil_parent(android fragment replace fiil_parent with frame layout)

根据图片,当我将我的片段替换为FrameLayout时,大小不合适。 例如我的页面按钮在主布局对象下 activity_main布局代码

<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" android:orientation="vertical" tools:context="com.teskaco.bbpos.Activities.MainActivities.MainActivity"> <com.alirezaafkar.toolbar.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:paddingTop="@dimen/app_bar_top_padding" app:direction="rtl" app:font="@string/font_path" app:navigationIcon="@drawable/m_e_n_u" app:optionsMenu="@menu/main_menu"> <com.teskaco.bbpos.CustumView.CustomTextView android:id="@+id/main_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/psp_name" android:textColor="@color/colorControlNormal" android:textSize="@dimen/small_text_size" /> </com.alirezaafkar.toolbar.Toolbar> <FrameLayout android:id="@+id/main_fragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <include android:id="@+id/buttonPanel" layout="@layout/main_top_bar" /> </LinearLayout>

我的片段布局xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/fragment_pay" tools:context="com.teskaco.bbpos.Activities.MainActivities.PayBillFragment"> <com.teskaco.bbpos.CustumView.CustomButton android:id="@+id/send_button" style="@style/LightRaiseColorButtonRippleStyle" android:layout_width="@dimen/_300sdp" android:layout_height="@dimen/button_height" android:layout_gravity="center" android:layout_marginTop="@dimen/_4sdp" android:gravity="center" android:text="@string/send_button" android:textColor="@color/disable_button" android:textSize="@dimen/button_text_size" /> <LinearLayout android:orientation="vertical" android:gravity="center|top" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" android:layout_marginTop="@dimen/_6sdp" android:background="@color/white"> <RelativeLayout android:id="@+id/skech_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_4sdp" android:background="@color/white"> <ImageView android:id="@+id/skech_image" android:layout_width="@dimen/_90sdp" android:layout_height="@dimen/_101sdp" android:layout_centerHorizontal="true" android:src="@drawable/ghabz" /> <com.teskaco.bbpos.CustumView.CustomTextView android:id="@+id/ghabz_textview" android:layout_width="@dimen/_250sdp" android:layout_height="wrap_content" android:layout_below="@+id/skech_image" android:layout_centerHorizontal="true" android:layout_marginTop="@dimen/_21sdp" android:gravity="center" android:text="@string/bill_text" android:textSize="@dimen/mediume_text_size" /> </RelativeLayout> <LinearLayout android:id="@+id/name_layout" android:layout_width="@dimen/_250sdp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_8sdp" android:background="@drawable/raduis_bg_edit_text" android:orientation="horizontal"> <com.teskaco.bbpos.CustumView.CustomTextInputLayout android:id="@+id/shenase_text_input_layout" android:layout_width="@dimen/_210sdp" android:layout_height="wrap_content" android:layout_margin="@dimen/_5sdp" android:theme="@style/TextLabel"> <com.teskaco.bbpos.CustumView.CustomEditText android:id="@+id/id_bill" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="@dimen/_6sdp" android:layout_marginRight="@dimen/_6sdp" android:layout_marginTop="@dimen/_4sdp" android:background="@null" android:gravity="right" android:hint="@string/id_Bill_text" android:inputType="number" android:paddingBottom="@dimen/_4sdp" android:textColor="@color/green_blue" android:textSize="@dimen/defult_text_size" /> </com.teskaco.bbpos.CustumView.CustomTextInputLayout> <android.support.v7.widget.AppCompatImageView android:layout_width="@dimen/_16sdp" android:layout_height="@dimen/_12sdp" android:layout_gravity="center" android:layout_marginLeft="@dimen/_5sdp" android:background="@drawable/user_id" /> </LinearLayout> <LinearLayout android:id="@+id/name_layout1" android:layout_width="@dimen/_250sdp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_8sdp" android:background="@drawable/raduis_bg_edit_text" android:orientation="horizontal"> <com.teskaco.bbpos.CustumView.CustomTextInputLayout android:id="@+id/payment_text_input_layout" android:layout_width="@dimen/_210sdp" android:layout_height="wrap_content" android:layout_margin="@dimen/_5sdp" android:theme="@style/TextLabel"> <com.teskaco.bbpos.CustumView.CustomEditText android:id="@+id/id_payment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="@dimen/_6sdp" android:layout_marginRight="@dimen/_6sdp" android:layout_marginTop="@dimen/_4sdp" android:background="@null" android:gravity="right" android:hint="@string/id_payment_text" android:inputType="number" android:paddingBottom="@dimen/_4sdp" android:textColor="@color/green_blue" android:textSize="@dimen/defult_text_size" /> </com.teskaco.bbpos.CustumView.CustomTextInputLayout> <android.support.v7.widget.AppCompatImageView android:layout_width="@dimen/_16sdp" android:layout_height="@dimen/_12sdp" android:layout_gravity="center" android:layout_marginLeft="@dimen/_5sdp" android:background="@drawable/user_id" /> </LinearLayout> <com.teskaco.bbpos.CustumView.CustomButton android:id="@+id/barcode_reader" style="@style/GrayRaiseColorButtonRippleStyle" android:layout_width="@dimen/_250sdp" android:layout_height="@dimen/button_height" android:layout_gravity="center" android:layout_marginTop="@dimen/_24sdp" android:gravity="center" android:text="@string/barcode_reader_text" android:textColor="@color/disable_button" android:textSize="@dimen/button_text_size" /> </LinearLayout> </LinearLayout>

替换代码

fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.main_fragment, PayBillFragment.newInstance()).addToBackStack("PayBill").commit();

according to picture when i replace my fragment into FrameLayout the the size is not fit. for example my button in page is under the main layout object activity_main layout code

<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" android:orientation="vertical" tools:context="com.teskaco.bbpos.Activities.MainActivities.MainActivity"> <com.alirezaafkar.toolbar.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:paddingTop="@dimen/app_bar_top_padding" app:direction="rtl" app:font="@string/font_path" app:navigationIcon="@drawable/m_e_n_u" app:optionsMenu="@menu/main_menu"> <com.teskaco.bbpos.CustumView.CustomTextView android:id="@+id/main_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/psp_name" android:textColor="@color/colorControlNormal" android:textSize="@dimen/small_text_size" /> </com.alirezaafkar.toolbar.Toolbar> <FrameLayout android:id="@+id/main_fragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <include android:id="@+id/buttonPanel" layout="@layout/main_top_bar" /> </LinearLayout>

my fragment layout xml code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/fragment_pay" tools:context="com.teskaco.bbpos.Activities.MainActivities.PayBillFragment"> <com.teskaco.bbpos.CustumView.CustomButton android:id="@+id/send_button" style="@style/LightRaiseColorButtonRippleStyle" android:layout_width="@dimen/_300sdp" android:layout_height="@dimen/button_height" android:layout_gravity="center" android:layout_marginTop="@dimen/_4sdp" android:gravity="center" android:text="@string/send_button" android:textColor="@color/disable_button" android:textSize="@dimen/button_text_size" /> <LinearLayout android:orientation="vertical" android:gravity="center|top" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" android:layout_marginTop="@dimen/_6sdp" android:background="@color/white"> <RelativeLayout android:id="@+id/skech_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_4sdp" android:background="@color/white"> <ImageView android:id="@+id/skech_image" android:layout_width="@dimen/_90sdp" android:layout_height="@dimen/_101sdp" android:layout_centerHorizontal="true" android:src="@drawable/ghabz" /> <com.teskaco.bbpos.CustumView.CustomTextView android:id="@+id/ghabz_textview" android:layout_width="@dimen/_250sdp" android:layout_height="wrap_content" android:layout_below="@+id/skech_image" android:layout_centerHorizontal="true" android:layout_marginTop="@dimen/_21sdp" android:gravity="center" android:text="@string/bill_text" android:textSize="@dimen/mediume_text_size" /> </RelativeLayout> <LinearLayout android:id="@+id/name_layout" android:layout_width="@dimen/_250sdp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_8sdp" android:background="@drawable/raduis_bg_edit_text" android:orientation="horizontal"> <com.teskaco.bbpos.CustumView.CustomTextInputLayout android:id="@+id/shenase_text_input_layout" android:layout_width="@dimen/_210sdp" android:layout_height="wrap_content" android:layout_margin="@dimen/_5sdp" android:theme="@style/TextLabel"> <com.teskaco.bbpos.CustumView.CustomEditText android:id="@+id/id_bill" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="@dimen/_6sdp" android:layout_marginRight="@dimen/_6sdp" android:layout_marginTop="@dimen/_4sdp" android:background="@null" android:gravity="right" android:hint="@string/id_Bill_text" android:inputType="number" android:paddingBottom="@dimen/_4sdp" android:textColor="@color/green_blue" android:textSize="@dimen/defult_text_size" /> </com.teskaco.bbpos.CustumView.CustomTextInputLayout> <android.support.v7.widget.AppCompatImageView android:layout_width="@dimen/_16sdp" android:layout_height="@dimen/_12sdp" android:layout_gravity="center" android:layout_marginLeft="@dimen/_5sdp" android:background="@drawable/user_id" /> </LinearLayout> <LinearLayout android:id="@+id/name_layout1" android:layout_width="@dimen/_250sdp" android:layout_height="wrap_content" android:layout_marginTop="@dimen/_8sdp" android:background="@drawable/raduis_bg_edit_text" android:orientation="horizontal"> <com.teskaco.bbpos.CustumView.CustomTextInputLayout android:id="@+id/payment_text_input_layout" android:layout_width="@dimen/_210sdp" android:layout_height="wrap_content" android:layout_margin="@dimen/_5sdp" android:theme="@style/TextLabel"> <com.teskaco.bbpos.CustumView.CustomEditText android:id="@+id/id_payment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="@dimen/_6sdp" android:layout_marginRight="@dimen/_6sdp" android:layout_marginTop="@dimen/_4sdp" android:background="@null" android:gravity="right" android:hint="@string/id_payment_text" android:inputType="number" android:paddingBottom="@dimen/_4sdp" android:textColor="@color/green_blue" android:textSize="@dimen/defult_text_size" /> </com.teskaco.bbpos.CustumView.CustomTextInputLayout> <android.support.v7.widget.AppCompatImageView android:layout_width="@dimen/_16sdp" android:layout_height="@dimen/_12sdp" android:layout_gravity="center" android:layout_marginLeft="@dimen/_5sdp" android:background="@drawable/user_id" /> </LinearLayout> <com.teskaco.bbpos.CustumView.CustomButton android:id="@+id/barcode_reader" style="@style/GrayRaiseColorButtonRippleStyle" android:layout_width="@dimen/_250sdp" android:layout_height="@dimen/button_height" android:layout_gravity="center" android:layout_marginTop="@dimen/_24sdp" android:gravity="center" android:text="@string/barcode_reader_text" android:textColor="@color/disable_button" android:textSize="@dimen/button_text_size" /> </LinearLayout> </LinearLayout>

replace code

fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.main_fragment, PayBillFragment.newInstance()).addToBackStack("PayBill").commit();

最满意答案

您必须使用Parent Layout作为RelativeLayout并使用RelativeLayout属性。

<com.alirezaafkar.toolbar.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:paddingTop="@dimen/app_bar_top_padding" app:direction="rtl" app:font="@string/font_path" app:navigationIcon="@drawable/m_e_n_u" app:optionsMenu="@menu/main_menu"> <com.teskaco.bbpos.CustumView.CustomTextView android:id="@+id/main_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/psp_name" android:textColor="@color/colorControlNormal" android:textSize="@dimen/small_text_size" /> </com.alirezaafkar.toolbar.Toolbar> <FrameLayout android:id="@+id/main_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/buttonPanel" /> <include android:id="@+id/buttonPanel" layout="@layout/tab_bottom_menus" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> </RelativeLayout>

You have to use Parent Layout as RelativeLayout and use RelativeLayout property.

<com.alirezaafkar.toolbar.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:paddingTop="@dimen/app_bar_top_padding" app:direction="rtl" app:font="@string/font_path" app:navigationIcon="@drawable/m_e_n_u" app:optionsMenu="@menu/main_menu"> <com.teskaco.bbpos.CustumView.CustomTextView android:id="@+id/main_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/psp_name" android:textColor="@color/colorControlNormal" android:textSize="@dimen/small_text_size" /> </com.alirezaafkar.toolbar.Toolbar> <FrameLayout android:id="@+id/main_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/buttonPanel" /> <include android:id="@+id/buttonPanel" layout="@layout/tab_bottom_menus" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> </RelativeLayout>

更多推荐