【问题标题】:Multiple row and column layout in androidandroid中的多行多列布局
【发布时间】:2013-10-03 19:08:43
【问题描述】:

我想在 Android 中创建以下布局。

在多种屏幕尺寸下实现此 UI 的最佳布局是什么?我尝试使用 LinearLayout 并设置 layout_weight,但是第二行将需要另一个 layout_weight,这对性能不利。我也尝试过使用 TableLayout,但是由于图像太大,所以看不到第三行。

我应该使用 LinearLayout 并为每个屏幕尺寸(mdpi、hdpi 等)创建图像吗?

感谢任何帮助。谢谢。

【问题讨论】:

  • 尝试相对布局
  • 正如我所见,最好的方法是带重量的线性布局。安全可靠且不那么复杂。否则你将有很多艰苦的工作和风险。在多个支持屏幕中

标签: android


【解决方案1】:

以下是您需要的 lyout。除非明智地使用,否则使用重量不是性能问题。仅当您使用太多带有重量的嵌套布局时,才会出现性能问题。每当我们开发支持多种分辨率的屏幕时,权重是最好的选择。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:weightSum="2">

        <ImageView
            android:layout_weight="1"
            android:id="@+id/img_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />

        <ImageView
            android:layout_weight="1"
            android:id="@+id/img_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

   <ImageView
        android:id="@+id/img_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher" />
</LinearLayout>

【讨论】:

  • 这是否意味着我必须为每个屏幕尺寸准备具有预定义宽度和高度的图像?我想如果使用的图像很大,它将占据大部分屏幕。
  • 是的,对于每种屏幕尺寸,您的 HDPI、MDPI 和 XHDPI 文件夹中必须有一组图像。
【解决方案2】:

试试这个方法。这是一款适用于所有屏幕的加权布局设计作品。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

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

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

输出

【讨论】:

    【解决方案3】:

    这里是布局的大纲

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <ImageView 
        android:id="@+id/image_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        android:background="@android:color/transparent"
        android:src="@drawable/abox"/>
    
    <LinearLayout 
        android:id="@+id/lin_lt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="10"
        android:orientation="horizontal"
        android:layout_below="@+id/image_1">
        <ImageView 
        android:id="@+id/image_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:scaleType="fitXY"
        android:src="@drawable/abox"
        android:background="@android:color/transparent"/>
        <ImageView 
        android:id="@+id/image_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:scaleType="fitXY"
        android:src="@drawable/abox"
        android:background="@android:color/transparent"/>
    </LinearLayout>
    <ImageView 
        android:id="@+id/image_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lin_lt"
        android:scaleType="fitXY"
        android:src="@drawable/abox"
        android:background="@android:color/transparent"/>
    
    </RelativeLayout>
    

    如果您为每个屏幕尺寸/密度组提供适当尺寸的图像,则可以摆脱 LinearLayout。对于每个屏幕尺寸/密度使用适当尺寸的图像或使用 9patch 图像总是更好的主意。

    【讨论】:

    • this link 建议您始终尽可能尝试使用 RelativeLayout 以确保所有屏幕尺寸/密度的完整性,并应为主要屏幕尺寸/密度组提供可绘制资源。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多