【问题标题】:Android complex layout design adviseAndroid复杂布局设计建议
【发布时间】:2013-10-31 13:32:43
【问题描述】:

我想为 Android 仪表板活动实现一个复杂的布局,如下图所示。

对于某些单元格,应该像这样添加多个文本视图, 例如:

这里将使用异步任务从 Web 服务加载一些文本视图的文本。只有一个图像视图应该可以点击。
我想知道在 Android 中实现这种复杂布局的最佳方法是什么。 我曾尝试使用带有 lianearlayouts 的线性布局来实现这一点,但它非常混乱而且非常复杂。然后我尝试将表格布局与 lianearlayouts 一起使用,但它也变得越来越复杂。然后我想到了使用带有线性布局的网格视图来做到这一点,但我想知道如何准确地做到这一点。
我仍处于学习阶段,我只是想要一个建议,什么是实现这种布局的最佳方法,我应该使用什么类型的布局以及在哪里使用。我不需要完整的实现。

提前致谢。

【问题讨论】:

标签: android android-layout android-intent android-fragments android-emulator


【解决方案1】:

使用 Android Percent Support Lib 可以很容易地解决这类问题,

结帐要点: https://gist.github.com/shekarrex/5458f697c02e5619b0e2

【讨论】:

    【解决方案2】:
    【解决方案3】:

    更新:我们知道,API 级别 26 已弃用百分比支持库。ConstraintLayout 是实现相同扁平 xml 结构的新方法。

    Updated Github Project

    更新示例:

    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/fifty_thirty"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ffff8800"
            android:gravity="center"
            android:text="@string/fifty_fifty_text"
            android:textColor="@android:color/white"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="0.5"
            android:textSize="25sp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.5" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="#ffff5566"
            android:gravity="center"
            android:text="@string/fifty_fifty_text"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="0.5"
            app:layout_constraintLeft_toRightOf="@id/fifty_thirty"
            app:layout_constraintTop_toBottomOf="@id/fifty_thirty"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.5" />
    
    </android.support.constraint.ConstraintLayout>
    

    已弃用 任何人都需要一个简单的演示来使用百分比支持库。

    CODE and CONCEPT HERE.

    <android.support.percent.PercentRelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/fifty_huntv"
            android:background="#ffff8800"
            android:text="50% - 50%"
            android:textColor="@android:color/white"
            style="@style/STYLE_PERCENT_TV"
            app:layout_heightPercent="50%"
            app:layout_widthPercent="50%" />
        <TextView
            android:id="@+id/comp2_twenty_fifty_tv"
            android:layout_toRightOf="@id/fifty_huntv"
            android:background="#ffff5566"
            android:text="20%-50%"
            style="@style/STYLE_PERCENT_TV"
            app:layout_heightPercent="20%"
            app:layout_widthPercent="50%"
            />
    
        <TextView
            android:id="@+id/comp2_ten_fifty_tv"
            android:layout_below="@id/comp2_twenty_fifty_tv"
            android:layout_toRightOf="@id/fifty_huntv"
            android:background="#aa3628cc"
            android:text="30%-50%"
            style="@style/STYLE_PERCENT_TV"
            app:layout_heightPercent="30%"
            app:layout_widthPercent="50%"
            />
        <TextView
            android:id="@+id/century_50_tv"
            android:layout_below="@id/fifty_huntv"
            android:background="#aacacc46"
            android:text="50%-100%"
            style="@style/STYLE_PERCENT_TV"
            app:layout_heightPercent="50%"
            app:layout_widthPercent="100%"
            />
    </android.support.percent.PercentRelativeLayout>
    

    WithIn 样式.xml

     <style name="STYLE_PERCENT_TV">
            <item name="android:height">0dp</item>
            <item name="android:width">0dp</item>
            <item name="android:gravity">center</item>
            <item name="android:textColor">@android:color/white</item>
            <item name="android:textSize">25sp</item>
        </style>
    

    这里的 Github 项目

    【讨论】:

      【解决方案4】:

      如果我需要实现这样的布局,我会将其拆分为 3 个部分:
      1. 截图中从上到下的一行红色TextView。
      2. 左栏从橙色“2 TextViews”一直到底部。
      3. 右列带有绿色和灰色的 TextView。

      对于每个部分,您至少有 3 个选择:
      1. 如果部件有一些严肃的逻辑,我会使用Fragments,因为它们有一个生命周期。
      2.如果逻辑小并且您不想在活动中实现所有内容,我会 使用custom View
      3. 如果只想拆分布局 xml,请检查 include 标签。
      4. 您可以使用 1 - 3 的任意排列,并以适合您需要的不同方式拆分布局。

      【讨论】:

        【解决方案5】:

        最好的方法是你可以使用 Fragmentsinclude 标签来添加其他 xml 布局

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多