【问题标题】:Layout weight not working in portrait mode布局权重在纵向模式下不起作用
【发布时间】:2013-01-26 03:37:36
【问题描述】:

遇到了一个我无法弄清楚的 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);
    }

虽然这在技术上总是对我有用,虽然它确实达到了相同的结果,但我仍然很好奇为什么地图部分的权重似乎被忽略了

【问题讨论】:

  • 您可能需要为纵向模式定义自定义布局

标签: android android-layout android-linearlayout


【解决方案1】:

尝试将layout_weight=".20" 添加到纵向模式的列表视图中

【讨论】:

  • 有趣的结果。将此权重应用于列表视图的容器实际上为列表提供了更多空间,而不是太多(足够我可以看到列表中的三个项目)。应用任何大于 0.2 的值似乎都没有任何效果。
【解决方案2】:

尝试将 weightSum="2" 添加到根 LinearLayout。

【讨论】:

  • 应用权重总和 2 并没有改变问题,但是当我添加权重总和 3 或 4 时,列表视图的容器变小(应该如此),但地图的容器变大(不应该)XD
【解决方案3】:

不管你给了多少重量。 由于权重总和将在根上应用,将始终视为 100%,并且您必须根据它对其进行划分,并在根布局中将其视为 100%。 并在子项或茎组件中应用布局权重,以根据根进行划分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    相关资源
    最近更新 更多