android - GridLayout 项目未正确对齐

标签 android xml

我有 3 个项目:

一个 ImageView ,一个 TextView 和一个复选框

我希望 ImageView (信息图标)出现在 GridLayout 的最左侧,文本出现在屏幕中央,复选框出现在最右侧,尽管设置了我的列正确,图像仍然堆叠在彼此之上,如图所示;有什么想法吗?

enter image description here

custome_row.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:alignmentMode="alignMargins"
    android:useDefaultMargins="true"
    android:columnOrderPreserved="false"
    android:rowCount="2"
    android:columnCount="3"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/rowImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_marginTop="6dp"
        android:src="@drawable/ic_info_outline" />

    <TextView
        android:id="@+id/rowTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:layout_row="0"
        android:layout_column="2"
        android:layout_marginLeft="8dp"
        android:textSize="20dp" />

    <CheckBox
        android:id="@+id/completedCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="0"/>

</GridLayout>

最佳答案

您的代码:您对 CheckBoxTextView 使用了相同的代码:

    android:layout_row="0"
    android:layout_column="2"

您还可以为此采用布局引力:

enter image description here

使用下面的代码:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:useDefaultMargins="true"
android:columnOrderPreserved="false"
android:rowCount="2"
android:columnCount="3"
android:orientation="horizontal">

<ImageView
    android:id="@+id/rowImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_row="0"
    android:layout_marginTop="6dp"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:layout_columnWeight="1"
    android:id="@+id/rowTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/colorPrimary"
    android:layout_row="0"
    android:text="TextView"
    android:layout_column="1"
    android:layout_marginLeft="8dp"
    android:textSize="20dp" />

<CheckBox
    android:layout_columnWeight="1"
    android:text="android"
    android:id="@+id/completedCheck"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="2"
    android:layout_row="0"/>

关于android - GridLayout 项目未正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49112181/

相关文章:

android - 如何在解析android上使用旧密码更改密码

java - Android Java Google Places GET(拉取)评论 JSONException 没有评论值(value)

android - Firebase:如何确保与各种版本的 Google Play 服务兼容?

java - 如何从 jackson Xml 解析中删除 namespace 定义

php - 使用 php 跳过 xml feed 中的某些对象

Android 应用程序 CPU 使用率高

java - com.w3c.dom.Document without <?xml version ="1.0"encoding ="UTF-8"standalone ="no"?>

python - 将 PMML 3.2 模型导入到 Python 中进行调试并转换为 4.2

c# - 尝试使用 LINQ 读取 xml 文件

android - 获取 html 时出现奇怪的 Jsoup OutOfBoundsException