簡體   English   中英

版式權重在縱向模式下不起作用

[英]Layout weight not working in portrait mode

遇到了一個我無法弄清的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相冊截圖

更新我可以實現所需的效果,但無需依賴重量屬性。 對於縱向布局,我將地圖視圖的容器的高度設置為等於顯示高度的一半,並且由於列表視圖的權重起作用,因此它將自動填充屏幕的剩余一半。

身高碼

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);
    }

盡管從技術上講這對我一直有效,並且確實可以達到相同的結果,但我仍然很好奇為什么地圖部分的權重似乎被忽略了

嘗試通過將layout_weight=".20"到縱向模式下的列表視圖

嘗試將weightSum =“ 2”添加到根LinearLayout中。

無關緊要的重量。 由於權重總和將以100%的比例通過根計算,因此您必須根據權重將其划分,認為根的分布比例為100%。 並在兒童或詞干成分中應用布局權重,以分別對應於根。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM