【发布时间】: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 此布局不完整,我尝试删除与问题无关的所有内容。现在问题解决了。但我同意我需要更多地了解布局。