它移至活动,但我得到一个空白页面
MainActivit.kt
这是我用来移动的方块
binding.fab2.setOnClickListener {
val intent = Intent(this, InfoActivity::class.java)
startActivity(intent)
}
其他活动
我把包裹拿走了
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.ConstraintLayou
t>
清单
我手动添加了另一个活动
<?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>
一切似乎都很好,但我不知道出了什么问题
我已经尝试过,没有绑定,也是同样的问题。尝试从同一个 Activity 移动到 MainActivity 并成功了
看起来
activity_info.xml
嵌套可能是问题所在。
你有
ConstraintLayout
AppBarLayout
MaterialToolBar
TextView
您的
textview
具有约束布局属性,但不在约束布局中。 AppBarLayout
没有约束布局属性。
我想你正在寻找类似的东西
CoordinatorLayout
AppBarLayout
MaterialToolbar
ConstraintLayout
TextView (or other content)
AppBarLayout
应与 CoordinatorLayout
配合(用于展开/折叠等),并且 TextView
应位于主要内容区域(ConstraintLayout
)
例如:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2023 Google LLC.
SPDX-License-Identifier: Apache-2.0
-->
<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"
android:fitsSystemWindows="true"
tools:context=".InfoActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<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" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>