繁体   English   中英

Android 布局:将一个布局粘贴到屏幕顶部并在下方居中放置另一个布局

[英]Android Layouts: Stick one Layout to the top of the screen and place another Layout centred below

我正在开发我的第一个 android-project,一个 Android Studio 中的记忆游戏。 gameActivity-Frame 应该包含一些 textViews,显示在屏幕顶部的一行中(用于统计数据)和下面的存储卡。 有多个内存版本,因此内存卡的数量会有所不同 - 以及他们的 GridLayout 的尺寸。

我的主要布局是一个 LinearLayout(垂直)。 我在它上面放置了另一个 LinearLayout(水平),Textviews 发生的地方。 为了显示卡片,我使用了 GridLayout,一旦游戏开始,它就会填充卡片。 LinearLayout(带有 textViews)应始终贴在屏幕顶部。 GridLayout 应位于下方剩余空间的中心。

我得到的是,两个布局要么坚持在中间,要么 LinearLayout 是正确的,但 GridLayout 要么位于剩余空间的底部,要么位于剩余空间的顶部。

我试图在 SO 上找到解决方案,我想我尝试了组件的gravitylayout_gravity设置的所有可能组合。 我可以想象问题是我可以在主布局中使用wrap_contentmatch_parent ,但不能同时使用两者。 猜猜我需要wrap_content将第一个布局粘贴到顶部,而match_parent需要使用此布局下方的整个空间。

如何将第一个布局粘贴到顶部并将 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>

使用 相对布局约束布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
    >

    <LinearLayout
        android:layout_alignParentTop="true"
        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:layout_centerInParent="true"
        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" />
</RelativeLayout >

您可以将相对布局与线性布局一起使用。根据您的问题,我知道您希望您的线性布局始终保持在顶部,而 GridLayout 始终保持在中心。如果是这种情况,那么我认为以下代码片段会对您有所帮助。

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

   </GridLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentTop="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Texts Here"
            android:layout_gravity="center"
            android:gravity="center"/>

    </LinearLayout>

</RelativeLayout>

ConstraintLayout 解决了我的问题......这是最终完美运行的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/memory_activity_4x4"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearLayout_4x4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        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/gridLayout4x4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:columnCount="4"
        android:foregroundGravity="center"
        android:orientation="horizontal"
        android:rowCount="4"
        android:textAlignment="center"
        android:useDefaultMargins="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout_4x4"
        tools:context=".GameActivity" />
</androidx.constraintlayout.widget.ConstraintLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM