You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin跳转至InfoActivity后页面空白问题求助

问题:跳转至InfoActivity后页面空白

从MainActivity跳转至InfoActivity操作成功,但页面显示空白。尝试过不使用ViewBinding的方式跳转,问题依旧;但从InfoActivity跳转回MainActivity正常。


相关代码及配置

MainActivity跳转代码

binding.fab2.setOnClickListener {
    val intent = Intent(this, InfoActivity::class.java)
    startActivity(intent)
}

InfoActivity代码

package ...

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.m.anwer.astudy.databinding.ActivityInfoBinding

public class InfoActivity : AppCompatActivity() {

    private var _binding: ActivityInfoBinding? = null

    private val binding: ActivityInfoBinding
        get() = checkNotNull(_binding) { "Activity has been destroyed" }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ActivityInfoBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }
}

activity_info.xml布局

<?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=".InfoActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light" />

        <TextView
            android:id="@+id/tv1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Welcome"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_margin="16dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </com.google.android.material.appbar.AppBarLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Manifest配置

<?xml version="1.0" encoding="utf-8"?>
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <application 
        android:allowBackup="true" 
        android:icon="@drawable/graduate_hat" 
        android:roundIcon="@drawable/graduate_hat" 
        android:label="@string/app_name" 
        android:supportsRtl="true" 
        android:theme="@style/AppTheme">
        <activity 
            android:name=".MainActivity" 
            android:exported="true">
            <intent-filter>
                <action 
                    android:name="android.intent.action.MAIN" />
                <category 
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".InfoActivity" />
    </application>
</manifest>

问题原因及解决办法

核心问题

布局里的TextView被放在AppBarLayout内部,但却使用了ConstraintLayout的约束属性(app:layout_constraintRight_toRightOf、app:layout_constraintTop_toTopOf)。AppBarLayout本质是垂直方向的LinearLayout,不识别ConstraintLayout的约束,导致这些属性失效,TextView的位置异常(大概率被Toolbar遮挡或未正确显示),加上布局里除了AppBarLayout和这个TextView外没有其他内容,视觉上就呈现空白。

解决方式二选一:

方式1:将TextView移到ConstraintLayout下

把TextView从AppBarLayout中移出,放到外层ConstraintLayout里,设置与AppBarLayout的约束关系,确保它在Toolbar下方显示:

<?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=".InfoActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light" />

    </com.google.android.material.appbar.AppBarLayout>

    <!-- 移到外层ConstraintLayout中 -->
    <TextView
        android:id="@+id/tv1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Welcome"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_margin="16dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/appbar" />

</androidx.constraintlayout.widget.ConstraintLayout>

方式2:保留TextView在AppBarLayout中,改用LinearLayout布局属性

如果要让TextView在AppBarLayout内部显示,替换ConstraintLayout的约束为LinearLayout支持的属性,比如用android:layout_gravity控制水平位置:

<TextView
    android:id="@+id/tv1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Welcome"
    android:textSize="18sp"
    android:textStyle="bold"
    android:layout_margin="16dp"
    android:layout_gravity="end" /> <!-- 替换约束为layout_gravity -->

另外,也可以检查下AppTheme是否设置为NoActionBar,避免默认ActionBar与MaterialToolbar冲突,但这不是导致空白的直接原因。


内容的提问来源于stack exchange,提问作者Mohamed Anwer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 07:05:53