我有 3 个项目:
一个 ImageView ,一个 TextView 和一个复选框
我希望 ImageView (信息图标)出现在 GridLayout 的最左侧,文本出现在屏幕中央,复选框出现在最右侧,尽管设置了我的列正确,图像仍然堆叠在彼此之上,如图所示;有什么想法吗?
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>
最佳答案
您的代码:您对 CheckBox
和 TextView
使用了相同的代码:
android:layout_row="0"
android:layout_column="2"
您还可以为此采用布局引力:
使用下面的代码:
<?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/