Android布局。使用<include>

时间:2017-10-10 09:52:51

标签: android android-layout

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

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

screenshot of the seventh column off the screen

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>

1 个答案:

答案 0 :(得分:1)

您可以使用下面的额外LinearLayout在“TableRow”标记内使用权重进行分区。

<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

的更多详情