简体   繁体   English

Android布局:如何避免嵌套权重?

[英]Android Layouts: How to avoid nested weights?

guys. 家伙。 I'm trying to code a layout for an activity which contains 4 different fragments, a list fragment and three detail-related fragments. 我正在尝试为包含4个不同片段,列表片段和三个与细节相关的片段的活动编写布局。 I try to make it look like the following diagram 我试着让它看起来像下图 在此输入图像描述

With LinearLayout I can get the 30/70 ratio between the list fragment and the detail area (where the other three fragments are supposed to be). 使用LinearLayout,我可以得到列表片段和细节区域(其他三个片段应该是)之间的30/70比率。 However, when dealing with these three fragments I really don't know how to keep them within the ratios I expect them to have, since you cannot nest layouts with weightSum attributes. 但是,当处理这三个片段时,我真的不知道如何将它们保持在我期望它们的比例之内,因为你不能使用weightSum属性嵌套布局。

I've been trying with RelativeLayouts but don't want to go with 'wrap_content' values, since some of the contents be bigger than others, breaking thus the appearance I'm trying to achieve. 我一直在尝试使用RelativeLayouts,但不想使用'wrap_content'值,因为有些内容比其他内容更大,因此破坏了我想要实现的外观。

Is there a way to do this? 有没有办法做到这一点? I know of the TableLayouts, but AFAIK they're like HTML tables: something to use with data, not as a lay out tool... 我知道TableLayouts,但是AFAIK它们就像HTML表格:用于数据的东西,而不是布局工具......

Nested weights should work just fine, I've used them a few times although eclipse shows a hint telling that "nested weights are bad for performance". 嵌套的权重应该可以正常工作,我已经使用了几次,尽管eclipse显示了一个提示“嵌套权重对性能不利”。

You should try something like: 你应该尝试类似的东西:

<LinearLayout android:id="@+id/main_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:weightSum="1"
    android:orientation="horizontal">

    <LinearLayout android:id="@+id/layout_fragment_a"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.5"/>

    <LinearLayout android:id="@+id/layout_container_b_c"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.5"
        android:weightSum="1"
        android:orientation="vertical">

        <LinearLayout android:id="@+id/layout_fragment_b"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="0.7"/>

        <LinearLayout android:id="@+id/layout_fragment_c"
            android:layout_height="0dp"
            android:layout_width="match_parent"
            android:layout_weight="0.3"/>

    </LinearLayout>

</LinearLayout>

And that's the way I've done it other times. 这就是我其他时候做过的方式。 The xml can have some failures and typos (writting rigth now here in the response box :P) but it should help you getting the idea: one main layout (main_layout) using full space and containing two second level layouts 50% width each one (fragment_a and container_b_C) and another tow layouts in onw of the second level layouts splitting the space in that layout 70/30 (fragment_b and fragment_c) :) xml可能有一些失败和拼写错误(现在写在响应框中的写法:P)但它应该有助于你理解:一个主要布局(main_layout)使用完整空间并包含两个二级布局每个50%宽度( fragment_a和container_b_C)以及第二级布局的onw中的另一个两个布局,分割该布局70/30(fragment_b和fragment_c)中的空间:)

Hope it helps! 希望能帮助到你!

Why not trying : 为什么不尝试:

<LinearLayout android_width="30.0" android_height="fill_parent"/>
<LinearLayout android_width="50.0" android_height="fill_parent"/>
<LinearLayout android_width="50.0" android_height="fill_parent">
      <LinearLayout android_width="fill_parent" android_height="70.0"/>
      <LinearLayout android_width="fill_parent" android_height="30.0"/>
</LinearLayout>

I didn't test it (not on my Eclipse worspace right now), but this should do the trick... 我没有测试它(现在不在我的Eclipse恶劣空间),但这应该可以解决问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM