片段在活动上重叠:Android Studio

问题描述

我单击活动上的按钮以调用该片段,以便它用新片段替换该活动,它不会用该片段替换该活动,但是该片段的内容在该活动上重叠了。

这是活动的kotlin文件

import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';
import Backend from 'i18next-http-backend';

i18n.use(Backend)
  .use(initReactI18next)
  .init({
    fallbackLng: "En",debug: true,returnObjectTrees: true,backend: {
      loadpath: function (path) {
        return path;
      }
    },react: {
      wait: true,useSuspense: false,},interpolation: {
    escapeValue: false,// not needed for react as it escapes by default
  }
  }).catch(() => console.log("Error setting language"));

活动的xml文件

package com.sahil.randoms
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_game_start.*

class ActivityGameStart : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_game_start)


        title="Start"

        btnStart.setonClickListener(){

            frameStart.removeAllViews()

            val fragmentManager= supportFragmentManager
            val transaction=fragmentManager.beginTransaction()
            transaction.replace(R.id.frameStart,FragmentPlayArea()).commit()

        }

    }
}

片段的kotlin文件

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".ActivityGameStart"
    android:id="@+id/rlStart">

    <TextView
        android:id="@+id/txtHighscore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="HIGH score"
        android:textColor="#000000"
        android:textSize="18sp"
        android:padding="10dp"
        android:background="@drawable/background"
        android:layout_margin="15dp"
        />
    <FrameLayout
        android:id="@+id/frameStart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/txtStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press the Start and Start the Timer"
        android:layout_below="@id/txtHighscore"
        android:layout_marginTop="50dp"
        android:layout_marginHorizontal="50dp"
        android:layout_centerHorizontal="true"
        android:textSize="25sp"
        android:textStyle="bold"
        android:textColor="#8f0077"
        />

    <Button
        android:id="@+id/btnStart"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtStart"
        android:layout_centerHorizontal="true"
        android:text="Start"
        android:textStyle="bold"
        android:layout_marginTop="80dp"
        android:textSize="20sp"
        android:background="@drawable/button"/>


</RelativeLayout>

片段的xml文件

package com.sahil.randoms

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class FragmentPlayArea : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ):View? {

        val view = inflater.inflate(R.layout.fragment_play_area,container,false)

        return view
    }
}

screenshot of the activity before calling fragmnet

after calling fragment i.e. overlapped fragment

请帮助我,因为一段时间以来我无法解决此问题:(

解决方法

您有一个“活动”布局,所有文本和按钮都布置在不同的位置,然后是FrameLayout,它填充了整个视图(高度和宽度的match_parent)。如果将某些内容放入该FrameLayout中,则将使所有文本和按钮您刚刚添加的新内容。

如果要执行此操作,而FrameLayout中的新内容用于隐藏其他内容,则需要做几件事:

  • FrameLayout在其他视图的下面(查看“开始”按钮仍在顶部)–您需要将其作为最后添加到XML文件的内容,因此在其他一切之上。或者也许给它一个elevation的值(不确定是否可以与RelativeLayout s一起使用,但它可能可以做到
  • 您的片段布局需要一个非透明的background值,否则您将通过它看到旧内容

就像@ Tenfour04所说的那样,通常的方法是让Activity布局具有一个内容容器(例如FrameLayout),然后将UI换入和换出-因此,从包含得分和开始按钮等的片段,当您单击按钮时,它将删除该片段并添加FragmentPlayArea片段。这样一来,看不见任何东西!而且它也可以与可访问性服务一起更好地工作(有时有时会卡在“隐藏的” UI上),如果您只是将其覆盖,它仍然存在

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...