【问题标题】:TextView inside fragment with layout_gravity set to center hides behind tabs in tabbed activity将layout_gravity设置为中心的片段内的TextView隐藏在选项卡式活动中的选项卡后面
【发布时间】:2021-05-21 12:33:42
【问题描述】:

我正在尝试在 Tabbed 活动中实现滚动片段。片段内只有一个TextView,只要layout_gravity 没有更改,它就可以正常工作。但只要将layout_gravity 设置为centerTextView 的内容就会隐藏在选项卡后面。

代码如下:

Fragment

<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Fragment_1">

    <TextView
        android:id="@+id/frag1_tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/text_margin"
        android:textSize="@dimen/text_medium"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:text="@string/buddha_vandana_mar" />

</androidx.core.widget.NestedScrollView>

还有ViewPager:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".PrayerActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:liftOnScroll="true"
        android:theme="@style/Theme.BuddhaPoojapathMarathi.AppBarOverlay">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"/>
    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

没有layout_gravity:center 如下所示:

使用layout_gravity:center 设置如下所示:

【问题讨论】:

    标签: android android-layout android-fragments layout-gravity


    【解决方案1】:

    在你的根布局中添加 android:fitsSystemWindows="true"。

      <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".Fragment_1"
        android:fitsSystemWindows="true"
    >
    

    【讨论】:

    • 能否在CoordinatorLayout中将viewPager的高度设置为0dp。如果你愿意,你可以使用约束布局而不是 CoordinatorLayout。 @VishalA。
    • 我按照您的建议调整了代码,但它并没有改变它的行为。此外,TextView 向上移动而不是向下移动。
    • 请更新您问题中的布局代码
    • 如果 CoordinatorLayout 中有 weight 属性。使用 0dp 高度。
    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多