【问题标题】:Android Layout. Make equal sized columns of a table layout with <include>安卓布局。使用 <include> 制作相同大小的表格布局列
【发布时间】:2017-10-10 18:38:28
【问题描述】:

我正在尝试使用包含块创建 7 个等宽列。以下代码似乎适用于更高的分辨率,但在较低的分辨率(例如 480x800)上,最后一列不在屏幕上。

我是 Android 新手,非常感谢任何关于如何组织可重用元素布局的建议。我的最终目标是一个可以包含叠加图像的 7x7“单元”板(这就是我在包含中使用 RelativeLayout 的原因)。

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.banzaitokyo.monstrarium.MainActivity">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="*">

        <TableRow android:background="@color/colorPrimary">

            <include layout="@layout/cell" />
            <include layout="@layout/cell" />
            <include layout="@layout/cell" />
            <include layout="@layout/cell" />
            <include layout="@layout/cell" />
            <include layout="@layout/cell" />
            <include layout="@layout/cell" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableLayout>
</LinearLayout>

cell.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorGround"
    tools:showIn="@layout/activity_main">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="1dp"
        android:adjustViewBounds="true"
        android:background="@color/colorDesert"
        android:contentDescription="@string/evil_state"
        android:cropToPadding="false"
        android:src="@drawable/evil" />
</RelativeLayout>

【问题讨论】:

  • 多么糟糕的布局嵌套!您知道嵌套布局不利于性能吗?作为一般规则,请尝试尽可能保持布局平坦
  • @ModularSynth 感谢您的评论。您会提出哪些具体的改进建议?你会怎么把它弄平?
  • @HareshChhelana 我看过了。它似乎不适用于 的情况。
  • @ModularSynth 此布局不完整,我尝试删除与问题无关的所有内容。现在问题解决了。但我同意我需要更多地了解布局。

标签: android android-layout


【解决方案1】:

您可以在“TableRow”标签中使用权重进行分区,并使用如下所示的额外 LinearLayout。

<TableRow android:background="@color/colorPrimary">

                    <include layout="@layout/cell"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"/>
                    <include layout="@layout/cell"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"/>
                    <include layout="@layout/cell"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"/>
                    <include layout="@layout/cell"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"/>
                    <include layout="@layout/cell"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"/>
                    <include layout="@layout/cell"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"/>
                    <include layout="@layout/cell"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"/>

            </TableRow>

你可以找到更多关于体重的细节here

【讨论】:

  • TableRow 继承自 LinearLayout。 不需要额外的LinearLayout。
  • 是的。你是对的@ModularSynth 感谢您升级我的知识。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多