得到了一個我無法弄清楚的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);
}
雖然這在技術上始終工作對我來說,雖然它確實達到同樣的效果,我仍然好奇,爲什麼在地圖部分的重量似乎忽略
可能您可能需要爲肖像模式定義自定義佈局 –