Android 在使用 fragment 时剪切我的文本

标签 android scrollview fragment

我有一个 fragment 的 Activity 。在 fragment 之上,我想放置小广告。问题是,如果我在 fragment 之上定义任何内容, fragment 中的文本将被剪切。查看图片(显示广告之前和之后)。

之前:Normal image 之后:Problematic image 请注意滚动条,它位于顶部,这意味着它降低了我的按钮的可见性。

我只想在广告可见时让文本“Jogar”可见。 如有必要,这里有一些 xml 和代码可以提供帮助。 如果您知道如何修复它,请帮助我。

activity_xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00F">
    <com.google.ads.AdView android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        ads:adUnitId="..."
        ads:adSize="BANNER"
        ads:testDevices="TEST_EMULATOR"
        android:background="@android:color/transparent"
        ads:loadAdOnCreate="true" />
    <FrameLayout android:id="@+id/fragmentPlaceholder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0F0"/>
</LinearLayout>

fragment _xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center">
        <Button android:id="@+id/playButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:text="@string/play"
            style="@style/TextFont"/>
        <Button android:id="@+id/rankButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rank"
            style="@style/TextFont"/>
        <Button android:id="@+id/achievementsButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/achievements"
            style="@style/TextFont" />
        <Button android:id="@+id/settingsButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/settings"
            style="@style/TextFont"/>
        <com.google.android.gms.common.SignInButton
            android:id="@+id/sign_in_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button android:id="@+id/sign_out_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@android:color/black"
            android:background="@android:drawable/btn_default"
            android:text="@string/logout"
            android:visibility="gone" />
    </LinearLayout>
</ScrollView>

在 Fragment 类中,我膨胀 View 调用:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.menu_initial, container, false);
    ...

在 Activity 类中,我调用 fragment :

fm = getSupportFragmentManager();
if(fm.getBackStackEntryCount() == 0){
    initialMenu = new InitialMenu();
    fm.beginTransaction().replace(R.id.fragmentPlaceholder, initialMenu, "initialFrag").commit();
}

最佳答案

尝试从 fragment 布局中的 ScrollView 包裹的 LinearLayout 中移除 android:layout_gravity="center" 属性:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"     
        android:gravity="center">
//...

该属性没有意义,因为您告诉 Linearlayout 将其自身置于父级中,父级为其提供所需/想要的空间(出于某些原因,使用该属性似乎可以抵消ScrollView 的内容)。

关于Android 在使用 fragment 时剪切我的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17029929/

相关文章:

Android 在可扩展 ListView 中禁用自动滚动

android - 某些设备中的 NullPointerException GoogleMaps

android - 保存/恢复 fragment 状态 android

android - 如何在 Jetpack Compose 中处理一次性操作?

java - 如何在 Java 中使用 XPath 从 XML 中获取特定节点?

android - 如何在 NestedScrollView 中显示滚动条

ios - 导航栏和工具栏顶部的 ScrollView ?

android - viewpager 中的多 Pane fragment

android - Google maps android on cluster items 点击不工作

Android GCM(推送通知): device doesn't receive notification if application is stopped