【问题标题】:Android: Optimize nested layoutsAndroid:优化嵌套布局
【发布时间】:2014-11-17 03:39:28
【问题描述】:

我创建了一个布局,但在我的 gridview 中遇到了很多性能问题。 我读过嵌套布局会阻止平滑滚动:

http://developer.android.com/training/improving-layouts/optimizing-layout.html

这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="100dp"
    android:layout_height="wrap_content">


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativelayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <com.bestpick.bestpickapplication.SquareImageView
            android:id="@+id/menu_item_image"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@color/LightGrey"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            />



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/menu_item_image"
            android:background="#77000000"
            android:orientation="vertical">
            <TextView
                android:id="@+id/menu_item_name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="1dp"
                android:textColor="@color/white"
                android:gravity="left"
                android:text="Title"
                android:textSize="12sp"
                android:maxLines="2"
                android:ellipsize="end"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical">

                <RatingBar android:id="@+id/menu_item_avg_rating"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:numStars="5"
                    android:stepSize="0.1"
                    android:rating="0"
                    style="?android:attr/ratingBarStyleSmall"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/white"
                    android:text= "("
                    android:textSize="12sp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/menu_item_number_ratings"
                    android:text= "0"
                    android:textColor="@color/white"
                    android:textSize="12sp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text= ")"
                    android:textColor="@color/white"
                    android:textSize="12sp"/>


            </LinearLayout>


        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

如果没有这么多层次的布局,我怎样才能达到同样的效果?

编辑:布局图片

【问题讨论】:

  • 你也可以给我们一张你想要的最终布局的照片,以便我们想出点什么吗?

标签: android android-layout gridview optimization


【解决方案1】:

我省略了多余的东西(例如文本颜色等),以便您可以看到如何使用布局属性来产生您想要的效果。此布局仅使用 6 个视图和一层嵌套——如果您真的想尽量减少视图数量,可以使用 5 个视图来实现,但这是更简洁的方法。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp" >

    <com.bestpick.bestpickapplication.SquareImageView
        android:id="@+id/menu_item_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <View android:background="#77000000"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignTop="@+id/menu_item_name"
        android:layout_alignParentBottom="true"/>

    <RatingBar
        android:id="@+id/menu_item_avg_rating"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

    <TextView
        android:id="@+id/menu_item_number_ratings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/menu_item_avg_rating" />

    <TextView
        android:id="@+id/menu_item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/menu_item_avg_rating" />

</RelativeLayout>

请注意,对于 RatingBar 旁边的 TextView,其目的是使用带有 %1$d 的字符串资源并使用 getString(R.string.whatever, some_integer) 来获取适当的文本。如果您不知道它是如何工作的,请参阅 this link

【讨论】:

    【解决方案2】:

    希望这会有所帮助 我的输出

    <LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="100dp"
    android:layout_height="wrap_content">
    
        <ImageView
            android:id="@+id/menu_item_image"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/darker_gray"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"/>
    
    
            <TextView
                android:id="@+id/menu_item_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:gravity="left"
                android:text="Title"
                android:textSize="12sp"
                android:maxLines="2"
                android:ellipsize="end"
                android:background="#770"
                />
            <LinearLayout
    
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#770"
                android:orientation="horizontal">    
                <RatingBar android:id="@+id/menu_item_avg_rating"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:numStars="5"
                    android:stepSize="0.1"
                    android:rating="0"
                    android:layout_weight="1"
                    style="?android:attr/ratingBarStyleSmall"
                    />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/menu_item_number_ratings"
                    android:text= "(0)"
                    android:textColor="@color/white"
                    android:textSize="12sp"
                    android:layout_weight="1"/>
                </LinearLayout>
    
            </LinearLayout>
    

    【讨论】:

    • 感谢您的帖子。 Text + 评级实际上必须“叠加”在图像顶部,以便两个底部重合。我应该如何编辑您的代码?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    相关资源
    最近更新 更多