【问题标题】:Android Layouts: Stick one Layout to the top of the screen and place another Layout centred belowAndroid 布局:将一个布局粘贴到屏幕顶部,并在下方居中放置另一个布局
【发布时间】: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_contentmatch_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


    【解决方案1】:

    使用relative layoutconstraint layout

    <?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 不应该在整个屏幕中居中,只能在 LinearLayout 下方的剩余空间中。我编辑了我的问题并添加了另一个屏幕截图。
    【解决方案2】:

    您可以将 相对布局与线性布局一起使用。从您的问题中,我了解到您希望您的线性布局始终保持在顶部,而 GridLayout 始终保持在中心。如果是这种情况,那么我认为以下代码sn-p 会帮助你。

    <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>
    

    【讨论】:

      【解决方案3】:

      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>
      

      【讨论】:

        猜你喜欢
        • 2017-07-06
        • 2020-02-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多