主要活动中的片段代码不会填满整个屏幕
问题描述:
我在主类中的FragmentLayout存在问题。我有位置的应用程序,在我的主要活动是地图(谷歌API),我有一个菜单,FragmentLayout(在我的主要活动XML)是wrap_content(高度),当我点击设置,FragmentLayout显示,但它不包括整个屏幕只是一个部分,并在设置下我看到地图(这不应该在那里)。当我为我的FragmentLaout设置match_parent时,我从主要活动中看不到地图。 这里是我的代码:主要活动中的片段代码不会填满整个屏幕
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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="com.globallogic.cityguide.MainActivity"
android:id="@+id/mainLayout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/navigation_action"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
// THIS IS MY FRAGMENTS:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" //here is this option which cover my whole screen when i put there match_parent
android:id="@+id/main_container"
android:layout_gravity="center"
>
</FrameLayout>
<include layout="@layout/activity_outdoor_map"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
app:menu="@menu/navigation_menu"
app:headerLayout="@layout/navigation_action"
android:layout_gravity="start"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
答
您正在使用不同的容器,而这个目的,你很可能宁愿希望只使用一个主容器(如您的XML表示),并更换片段在里面。
这样,您可以设置您的FrameLayout以适合整个屏幕,并在必要时替换设置Fragment和Map片段。
所以应该在您的MainActivity中更改代码。 我们假设你的MainActivity已经延伸AppCompatActivity,我们有mapFragment的实例,那么你一定想先加载地图碎片,像这样:当您按下设置按钮,您将只需更换
// Add the fragment to the 'main_container' FrameLayout
getSupportFragmentManager().beginTransaction().add(R.id.main_container, mapFragment).commit();
现在
片段:
SettingsFragment settingsFragment = new SettingsFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the main_container view with this fragment,
// and add the transaction to the back stack if you want the user to be able to navigate back
transaction.replace(R.id.main_container, settingsFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
更多信息:here
答
您可以在布局文件添加此。和其他菜单选项从你的Java文件。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="342dp"
android:layout_height="473dp" android:id="@+id/map" tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>
</LinearLayout>
为你的'FrameLayout'赋予''1''权重,并将高度设置为'0'。 – PPartisan
用RelativeLayout替换LinearLayout – zombie
我不明白你不想要的内容。 无论如何,组件的高度设置为match_parent时的行为是填充父视图中可用的所有空间。 如果你不想要某些东西被重叠,并且你想要控制每个元素的相对位置,你应该考虑使用RelativeLayout和layout_above以及layout_below,但是再一次,你的问题还不够清楚,我们不能正确回答它。 – c0rtexx