【发布时间】:2019-11-27 19:27:14
【问题描述】:
我正在开发我的第一个 android 项目,这是一个 Android Studio 中的记忆游戏。 gameActivity-Frame 应该包含一些 textView,显示在屏幕顶部的一行(用于统计信息)和下面的存储卡中。有多个内存版本,因此内存卡的数量会有所不同 - 以及它们的 GridLayout 的尺寸。
我的主要布局是一个线性布局(垂直)。我在其上放置了另一个 LinearLayout(水平),Textviews 发生的地方。为了显示卡片,我使用了 GridLayout,一旦游戏开始,它就会被卡片填充。 LinearLayout(带有 textViews)应该始终贴在屏幕顶部。 GridLayout 应位于下方剩余空间的中心。
我得到的是两个布局要么粘在中间,要么 LinearLayout 是正确的,但 GridLayout 要么在底部,要么在剩余空间的顶部。
我试图在 SO 上找到解决方案,我想我尝试了 gravity- 和 layout_gravity - 组件设置的所有可能组合。我可以想象问题是我可以在主布局中使用wrap_content 或match_parent,但不能同时使用两者。我想我需要wrap_content 将第一个布局粘贴到顶部,match_parent 才能使用此布局下方的整个空间。
如何将第一个 Layout 粘贴到顶部并将 GridLayout 置于剩余空间的中心?
图片显示了我当前的布局(参见下面的代码)以及我希望它的外观。 center_GridLayout
编辑澄清: LinearLayout 应该贴在屏幕顶部,GridLayout 应该是 在LinearLayout下方的剩余空间中居中。 RelativeLayout 无法正确完成这项工作,因为布局可能会重叠,具体取决于存储卡的数量和用户设备的尺寸。 我上传了另一个屏幕截图,可以更好地看到我想要实现的目标:
这是我的 .xml(删除了一些不重要的 textViews):
<?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/MemoryLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/textStatsPairs"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/textStatsPairs" />
</LinearLayout>
<GridLayout
android:id="@+id/gridLayoutMainActivityTEST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="4"
android:foregroundGravity="center"
android:orientation="horizontal"
android:rowCount="4"
android:textAlignment="center"
android:useDefaultMargins="true"
tools:context=".GameActivity" />
</LinearLayout>
【问题讨论】:
标签: android xml android-layout