【发布时间】:2018-07-28 14:04:08
【问题描述】:
我一直在努力创建一个响应式 UI 布局,它使用 GridLayout 将数字保存在 TextViews 中。我尝试使用 ConstraintLayout、Linear 和 Relative 作为背景,并尝试为 TextViews 使用不同的值来对齐它们. (该程序应该使用空的 TextViews 并单击以显示一个数字(最多 2 位数字))。以下是不同屏幕尺寸的布局图片:This is the activity on Nexus 5(5,0inch)
and this is on Nexus 1(3,7inch
此时这是 xml 的一部分(只上传一部分,因为我得到了相等的 TextViews)
<RelativeLayout
android:id="@+id/relLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
</RelativeLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingStart="34dp"
android:paddingEnd="48dp"
android:paddingTop="48dp"
android:paddingBottom="30dp"
android:layout_weight="6"
android:rowCount="5"
android:columnCount="5">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"
android:layout_gravity="center"
android:textSize="30sp"
android:layout_column="0"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
></TextView>
任何想法如何正确对齐网格和文本视图,以便它们在任何屏幕尺寸上工作?提前致谢。
【问题讨论】:
-
您可以使用 GridView 来设置属性
android:numColumns来设置列数,并使用自定义适配器来设置每个单元格的内容。列将相等,如果您在您的适配器项目中将使用match_parent宽度参数。 -
@grabarz121 自定义适配器到底是什么意思,因为我没有经验也无法理解你?
-
GridView 需要作为适配器类来填充自己。 Here's 一个例子,怎么做。您可以用空字符串填充适配器,并通过在现有 GridView 布局项上调用 onItemClickListener 来更改空值(例如,使用额外的 EditText 从用户输入更改单击位置的值)
标签: android xml user-interface layout