2013-01-23 50 views
0

得到了一個我無法弄清楚的Android問題,所以我在這裏要求另一組眼睛:D。我有一個片段,我試圖在橫向上並排顯示地圖和列表視圖,在縱向模式下時自上而下顯示。現在,當它處於橫向模式時,它的運行方式與預期相同,但由於某種原因,當它處於縱向模式時,地圖所佔空間超過我列出的重量。思考?下面是橫向和縱向的xml佈局,以及片段代碼和當前效果的一些截圖。根據我的經驗,它應該像我期望的那樣工作,而不是現在的情況。再次感謝! :)佈局權重不能在肖像模式下工作

XML景觀

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayoutMapSearch" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/npecCarbon" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 
    <FrameLayout 
     android:id="@+id/placeHolderMapView" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 
    </FrameLayout> 
    <LinearLayout 
     android:id="@+id/LinearLayout2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" > 
     <ListView 
      android:id="@+id/listViewTicketSearchResults" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
     </ListView> 
     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="@string/label_no_data" 
      android:textAppearance="@android:style/TextAppearance.Medium" 
      android:textColor="@color/npecWhite" 
      android:visibility="gone" > 
     </TextView> 
    </LinearLayout> 
</LinearLayout> 

XML肖像

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayoutMapSearch" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/npecCarbon" 
    android:orientation="vertical" > 
    <FrameLayout 
     android:id="@+id/placeHolderMapView" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 
    </FrameLayout> 
    <LinearLayout 
     android:id="@+id/LinearLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 
     <ListView 
      android:id="@+id/listViewTicketSearchResults" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
     </ListView> 
     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="@string/label_no_data" 
      android:textAppearance="@android:style/TextAppearance.Medium" 
      android:textColor="@color/npecWhite" 
      android:visibility="gone" > 
     </TextView> 
    </LinearLayout> 
</LinearLayout> 

片段代碼

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    if(mapFragment == null) { 
     mapFragment = SupportMapFragment.newInstance(); 
     mapFragment.setRetainInstance(true); 
     FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction(); 
     fragmentTransaction.add(R.id.placeHolderMapView, mapFragment); 
     fragmentTransaction.commit(); 
    } 
    if(mapResults == null) { 
     mapResults = new ArrayList<String>(); 
     mapResults.add("One"); 
     mapResults.add("Two"); 
     mapResults.add("Three"); 
     mapResults.add("Four"); 
    } 
    ArrayAdapter<String> simpleAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice, mapResults.toArray(new String[mapResults.size()])); 
    listViewSearchResults.setAdapter(simpleAdapter); 
} 

個截圖 Imgur album of screenshots

更新 我能達到我想要的效果,而且不依賴於權重屬性。對於肖像佈局,我給容器的地圖視圖的高度等於顯示高度的一半,並且由於列表視圖的權重正在工作,它會自動填充屏幕的剩餘一半。

身高代碼

private void calculateNewHeightForMap() { 
    Display display = getActivity().getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    if(size.y > size.x) { //We're in portrait mode, calculate height 
    LayoutParams mapParams = new LayoutParams(LayoutParams.MATCH_PARENT, size.y/2); 
    frameLayoutMapPlaceHolder.setLayoutParams(mapParams); 
    } 

雖然這在技術上始終工作對我來說,雖然它確實達到同樣的效果,我仍然好奇,爲什麼在地圖部分的重量似乎忽略

+0

可能您可能需要爲肖像模式定義自定義佈局 –

回答

1

嘗試在肖像模式下添加layout_weight=".20"列表視圖

+0

有趣的結果。將這個權重應用於listview的容器實際上給了列表更多的空間,而不是太多(足以讓我可以看到列表中的三個項目)。應用大於0.2的任何東西似乎都沒有影響。 – miosan

0

嘗試在根LinearLayout中添加weightSum =「2」。

+0

應用2的權重總和沒有改變問題,但是當我添加3或4的權重總和時,listview的容器變小(因爲它應該),但是地圖的容器變大了(當它不應該' t)XD – miosan

0

不重要的是什麼重量總和遺傳。 由於重量總和U將適用於根本將考慮爲100%,通過出,並且你必須根據它來分割它,考慮它是根本佈局100%。 並且根據孩子的不同來區分幼兒園或幼兒園組成部分的重量。