android - 如何使 TableLayout 在 ConstraintLayout 中垂直居中,并在其下方放置一些东西?
问题描述
我正在尝试在内部创建一个TableLayout
和AdMob
组件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 制作时,布局将出现在屏幕顶部。我不能给它一个固定的高度,因为我正在动态添加行。
我怎么解决这个问题?
解决方案
使 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>
推荐阅读
- python - 根据需要在同一行中使用双 if 语句
- ruby-on-rails - 未定义 MethodError for 'location' for nil:NilClass in rails during Rake Command
- twilio - 将现有号码添加到 Twilio Connect SID
- javascript - Object.entries 不是函数
- java - 从 Spring Boot 中的类路径加载嵌套资源
- python - 请求 for 循环和列表推导的 NumPy/SciPy 向量化替换
- pyspark - 如何根据日期从单行数据框中创建包含 2 行的数据框
- arrays - 如何为数组中的每个对象添加一个参数?
- json - React-Native 推送到 AWS S3
- kotlin - 在 Kotlin 中使用 Klaxon for JSON 和 Arrow for Option?