在没有嵌套布局的情况下进行此布局的最佳方法

时间:2014-07-28 21:53:19

标签: android android-layout layout android-fragments

根据我对布局管理器的模糊理解,我认为布局中的嵌套布局可能效率低下(这对于Android也是如此吗?)

我正在尝试进行此布局,但必须进行下一个布局。是否有一个更好的布局管理器,我可以使用,以便事情不嵌套?所有组件目前都是碎片,所以它是一个布局碎片而不是单个组件的情况。

enter image description here

由于

2 个答案:

答案 0 :(得分:1)

我不相信有一种方法可以在没有嵌套布局的情况下获得外观。如果是这样,那是因为您正在做很多可能不必要的图形(不知道您的其他要求)。通过"很多图形"我的意思是从你的图像,如果你想在每个框中的动态内容,那么你必须使用嵌套的布局,除非你有一个布局,然后重新绘制屏幕的分数部分,以给出嵌套布局的效果。

除非您遇到性能问题,否则您不必担心布局数量较少(即小于100左右)的效率。我有几个具有多层嵌套布局的应用程序(最多10个图层嵌套)并且在性能方面没有可测量的差异。

如果您有一个包含数百个子项的ScrollView或ListView,那么您可能会关注嵌套布局。我已经与其中的一些合作,它可以有所作为。例如,我有一个包含5,000个左右项目的ListView,每个项目有大约5个布局。当我把它降到3时,我发现后端有区别。这两个用户都没有注意到。 (不,我无法有效地回收视图容器,因为每个项目基本上都需要它自己的布局 - 部分回收是可能的。)

答案 1 :(得分:1)

虽然设置起来有点复杂,但您可以使用GridLayout来实现这一目标。假设顶视图和底视图具有固定高度,则类似于:

<?xml version="1.0" encoding="utf-8"?>

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:useDefaultMargins="true">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:background="#ffff070c"
        android:layout_gravity="top"></FrameLayout>

    <FrameLayout
        android:layout_width="200dp"
        android:layout_row="1"
        android:layout_column="0"
        android:background="#ff12ff1a"
        android:layout_rowSpan="2"
        android:layout_gravity="fill_vertical"></FrameLayout>

    <FrameLayout
        android:layout_height="64dp"
        android:layout_row="1"
        android:layout_column="1"
        android:background="#ff0906ff"
        android:layout_gravity="fill_horizontal"></FrameLayout>

    <FrameLayout
        android:layout_row="2"
        android:layout_column="1"
        android:background="#ff737373"
        android:layout_gravity="fill"></FrameLayout>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_row="3"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:background="#ffbebebe"></FrameLayout>
</GridLayout>

产生这个:

enter image description here