【问题标题】:I created an adaptive layout, it's working fine but i know it's not very good, So, need a better solution我创建了一个自适应布局,它工作正常,但我知道它不是很好,所以,需要一个更好的解决方案
【发布时间】:2020-06-12 06:38:43
【问题描述】:

我创建的一个适应性很小,但出于性能原因,我觉得它不是很有用。我添加了一些带有权重的空线性布局,以在顶部、左侧、右侧、中间和底部添加自适应边距。但我认为可能还有其他解决方案可以摆脱那些空布局,考虑到布局是自适应的。在此先感谢:)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".StacksGameActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1">

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:orientation="horizontal"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_margin="2dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_margin="2dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1">

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="4"
    android:gravity="center"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_margin="2dp">


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button_n" />

        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_margin="2dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_margin="2dp"
            android:background="@color/royal_blue_color">

        </FrameLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="1">

</LinearLayout>

我附上了一张图片来解释这个布局 请建议我是否可以使用其他任何东西使它看起来像这个布局而不添加那些空布局

【问题讨论】:

  • 你对其他android布局做过一些研究吗?你有什么特别的挣扎吗?你还没有真正解释你的问题是什么,你只是想要不同的解决方案:)
  • 我想要的是,考虑到布局对不同屏幕尺寸的适应性,出于性能原因优化我的布局。这个是自适应的,但它可能导致我认为必须有一些解决方案来删除那些空布局

标签: android android-layout android-linearlayout


【解决方案1】:

您可以使用 GridLayoutManager 来摆脱可能会影响性能的空 LinearLayouts。

你可以在资源中设置一个整数:

<integer name="grid_column_count">1</integer>

并在代码中检索:

int gridColumnCount = 
        getResources().getInteger(R.integer.grid_column_count);

并使用它:

mRecyclerView.setLayoutManager(new 
                     GridLayoutManager(this, gridColumnCount));

将您的项目设置为居中,然后通过将其值放置在不同的资源目录中,根据设备宽度为整数 grid_column_count 设置不同的值。

这样您就不会使用空的线性布局,并且可以根据设备宽度使用不同的列数实现相同的外观。

您可以在 Android fundamentals 05.3: Adaptive layouts 代码实验室中查看完整示例。

【讨论】:

    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多