2016-11-14 89 views
0

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

enter image description here

+0

爲你的'FrameLayout'賦予''1''權重,並將高度設置爲'0'。 – PPartisan

+0

用RelativeLayout替換LinearLayout – zombie

+0

我不明白你不想要的內容。 無論如何,組件的高度設置爲match_parent時的行爲是填充父視圖中可用的所有空間。 如果你不想要某些東西被重疊,並且你想要控制每個元素的相對位置,你應該考慮使用RelativeLayout和layout_above以及layout_below,但是再一次,你的問題還不夠清楚,我們不能正確回答它。 – c0rtexx

回答

0

您正在使用不同的容器,而這個目的,你很可能寧願希望只使用一個主容器(如您的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

0

您可以在佈局文件添加此。和其他菜單選項從你的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>