当动态替换framelayout时,片段的布局会崩溃

时间:2018-02-16 03:49:30

标签: android android-layout android-fragments

我有一个带有MainActivity的应用程序,其中包含一个FrameLayout,它将被一个片段替换,该片段基于在底部布局中单击的项目。我有一个名为"预订"当预约"预约时,必须出现在身体区域内。单击选项。 "预订" fragment包含一个微调器和一个按钮,它们位于RelativeLayout内部的屏幕中心。当片段的布局直接放在MainActivity的主体内部时,微调器和按钮就会按照我想要的方式居中。

enter image description here

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottompanel"
    android:orientation="vertical">

    <!-- The FrameLayout goes here -->

    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <Spinner
            android:id="@+id/city_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_above="@+id/center_space"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:dropDownVerticalOffset="20dp"
            android:dropDownWidth="match_parent"
            android:popupBackground="#d5ddea"
            android:spinnerMode="dropdown" />

        <Space
            android:id="@+id/center_space"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_centerInParent="true"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Done"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/center_space"
            android:onClick="doneButton" />

    </RelativeLayout>

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:weightSum="3"
    android:layout_weight="0"
    android:id="@+id/bottompanel">
    <LinearLayout
        android:id="@+id/profile_button"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1"
        android:clickable="true">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_profile"
            android:layout_gravity="center"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Profile"
            android:layout_gravity="center"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/bookings_button"
        android:clickable="true"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_bookings"
            android:layout_gravity="center"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Bookings"
            android:layout_gravity="center"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/games_button"
        android:clickable="true"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_tournaments"
            android:layout_gravity="center"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Games"
            android:layout_gravity="center"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

    </LinearLayout>
</LinearLayout>
</RelativeLayout>

但是当我用片段替换FrameLayout时,微调器消失,按钮在顶部移动。

enter image description here

activity_main.xml中的代码:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottompanel"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_frame"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:weightSum="3"
    android:layout_weight="0"
    android:id="@+id/bottompanel">
    <LinearLayout
        android:id="@+id/profile_button"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1"
        android:clickable="true">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_profile"
            android:layout_gravity="center"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Profile"
            android:layout_gravity="center"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/bookings_button"
        android:clickable="true"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_bookings"
            android:layout_gravity="center"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Bookings"
            android:layout_gravity="center"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/games_button"
        android:clickable="true"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_tournaments"
            android:layout_gravity="center"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Games"
            android:layout_gravity="center"
            android:layout_weight="1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">

    </LinearLayout>
</LinearLayout>
</RelativeLayout>

fragment_bookings.xml中的代码:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<Spinner
    android:id="@+id/city_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/center_space"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:dropDownVerticalOffset="20dp"
    android:dropDownWidth="match_parent"
    android:popupBackground="#d5ddea"
    android:spinnerMode="dropdown" />

<Space
    android:id="@+id/center_space"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_centerInParent="true"/>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Done"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/center_space"
    android:onClick="doneButton" />

</RelativeLayout>

bookings.java的onCreateView代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_bookings, container, false);
    Spinner loc = (Spinner)v.findViewById(R.id.city_spinner);
    String[] cities = new String[]{
            "Select a City",
            "ABC",
            "DEF",
            "GHI",
            "JKL",
            "MNO"
    };

    // Initializing an ArrayAdapter
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
            this.getActivity(),android.R.layout.simple_spinner_item,cities
    );
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    loc.setAdapter(spinnerArrayAdapter);
    loc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
            L1 = id;
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
    return v;

}

MainActivity.java中的代码:

package com.example.user.temporary;

import android.app.Activity;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements
        profile.OnFragmentInteractionListener,
        bookings.OnFragmentInteractionListener,
        games.OnFragmentInteractionListener {
    private Activity myActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.myActivity = this;
        LinearLayout profilebtn = (LinearLayout)this.findViewById(R.id.profile_button);
        LinearLayout bookingsbtn = (LinearLayout)this.findViewById(R.id.bookings_button);
        LinearLayout gamesbtn = (LinearLayout)this.findViewById(R.id.games_button);
        profilebtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                profile profileFragment = new profile();
                FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
                trans.replace(R.id.fragment_frame, profileFragment);
                trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                trans.commit();
            }
        });
        bookingsbtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                bookings bookings_frame = new bookings();
                FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
                trans.replace(R.id.fragment_frame, bookings_frame);
                trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                trans.commit();
            }
        });
        gamesbtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                games games_frame = new games();
                FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
                trans.replace(R.id.fragment_frame, games_frame);
                trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                trans.commit();
            }
        });
    }

    @Override
    public void onFragmentInteraction(Uri uri){

    }

}

2 个答案:

答案 0 :(得分:3)

height的{​​{1}}从FrameLayout更改为match_parent

wrap_content

答案 1 :(得分:1)

片段布局的高度为match_parent。尝试将其更改为wrap_content并查看其是否有效