【问题标题】:Fragment code in main activity does not fill the whole screen主要活动中的片段代码没有填满整个屏幕
【发布时间】:2016-11-16 19:24:49
【问题描述】:

我在主类中的 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>

【问题讨论】:

  • 给你的FrameLayout 一个weight1,并将高度设置为0
  • 将LinearLayout替换为RelativeLayout
  • 我不明白你不想要的内容。无论如何,当组件的高度设置为 match_parent 时,组件的行为是填充父视图中的所有可用空间。如果您不希望某些内容重叠并且您想控制每个元素的相对位置,您应该考虑将 RelativeLayout 与 layout_above 和 layout_below 一起使用,但同样,您的问题还不够清楚,我们无法正确回答。
  • 我有定位应用程序,在我的主要活动中是地图(谷歌 API),我有一个菜单,FragmentLayout(在我的主要活动 XML 中)是 wrap_content(高度),当我点击设置时, FragmentLayout 显示但它不覆盖整个屏幕只是一部分,在设置下我看到地图(不应该在那里)。当我为我的 FragmentLaout 设置 match_parent 时,我在我的主要活动中看不到地图。以前的答案更改权重和 relativeLayout 不起作用...

标签: android android-layout android-fragments


【解决方案1】:

您正在使用不同的容器,而为此您可能宁愿只使用一个主容器(如您的 xml 所说)并替换其中的片段。

这样您可以将 FrameLayout 设置为适合整个屏幕,并在必要时替换设置 Fragment 和 Map 片段。

所以应该在 MainActivity 中更改代码。 假设您的 MainActivity 已经扩展了 AppCompatActivity 并且我们有一个 mapFragment 实例,那么您需要先加载您的 Map Fragment,如下所示:

// 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

【讨论】:

    【解决方案2】:

    您可以在布局文件中添加它。以及您的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      相关资源
      最近更新 更多