android - 添加 ConstraintLayout 后工具栏不可见
问题描述
我正在使用工具栏将 ConstraintLayout 添加到 LinearLayout。添加 ConstraintLayout 会导致工具栏不再可见,并且 Layout Inspector 显示工具栏具有可见性,GONE
尽管调试器显示VISIBLE
. 我在这里错过了什么吗?使用LayoutParams.WRAP_CONTENT
也不行。
protected void setView(View view) {
super.setContentView(R.layout.content_view);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
LinearLayout layout = findViewById(R.id.linear_layout);
layout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
内容视图.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linear_layout"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
</LinearLayout>
传入的视图:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/white">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
你有一些你可能会丢失的东西。
- 图片不完整 - 为什么要动态添加视图?尝试将其添加到布局 xml 中
<include layout="@layout/[NAME_OF_THE_VIEW_LAYOUT]"/>
并注释掉 java 代码 - 只是为了确定。 - 您正在添加到 LinearLayout,但使用 ViewGroup.LayoutParams,这是不行的,请改用 LinearLayout.LayoutParams。
- 这可能与缺少的工具栏无关,但在 ConstraintLayout 内部可能没有任何内容。放置一个简单的文本视图或将颜色更改为非常明显的颜色(例如#00F)以确保添加它。
推荐阅读
- python - 如何在 Python 中沿圆形路径粘贴图像?
- javascript - Javascript:一个对象怎么可能是假的?
- excel - 将 Excel SUMPRODUCT 与多条件 COUNTIF 相结合
- angular - Angular 错误页面组件使用带有 id 的路由而没有重定向
- animation - 使用按钮交互重放 ThreeJs 动画
- android - Android MVVM 对话框视图模型
- elasticsearch - 是否可以为弹性搜索索引中的属性设置默认数据类型
- flutter - 无法使用冻结包生成 classname.g.dart 类
- jsf - inputNumber 不起作用,即允许使用字母
- powershell-2.0 - 使用 PowerShell 搜索驱动器上的所有文件夹和子文件夹