首页 > 解决方案 > 如何使 TableLayout 在 ConstraintLayout 中垂直居中,并在其下方放置一些东西?

问题描述

我正在尝试在内部创建一个TableLayoutAdMob组件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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".relax.LevelsUX">
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
           <TableLayout
              android:id="@+id/relax_level_ux_TL"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:stretchColumns="*">
            </TableLayout>

            <com.google.android.gms.ads.AdView
              android:id="@+id/relax_gameplay_adView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal|bottom"
              app:adSize="BANNER"
              app:adUnitId="ca-app-pub-3940256099942544/6300978111">
            </com.google.android.gms.ads.AdView> 
     </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

但问题是广告组件不可见,正如我android:layout_height="match_parent"为 TableLayout 提供的那样。现在,当我android:layout_height="wrap_content"为 TableLayout 制作时,布局将出现在屏幕顶部。我不能给它一个固定的高度,因为我正在动态添加行。

我怎么解决这个问题?

标签: android

解决方案


使 tablelayout 高度为 0dp, layout_weight=1 ,它将起作用。

       <TableLayout
          android:id="@+id/relax_level_ux_TL"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:gravity="center"
          android:stretchColumns="*">
        </TableLayout>

推荐阅读