添加 ConstraintLayout 后工具栏不可见
Toolbar not visible after adding ConstraintLayout
我正在将 ConstraintLayout 添加到带有工具栏的 LinearLayout。添加 ConstraintLayout 会导致工具栏不再可见,布局检查器显示工具栏可见性 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));
}
content_view.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>
您可能缺少一些东西。
- 图片不完整 - 为什么要动态添加视图?尝试使用
<include layout="@layout/[NAME_OF_THE_VIEW_LAYOUT]"/>
在布局 xml 中添加它并注释掉 java 代码 - 只是为了确定。
- 您正在添加到 LinearLayout,但使用 ViewGroup.LayoutParams,这不可行,请改用 LinearLayout.LayoutParams。
- 这可能与缺少工具栏无关,但ConstraintLayout 内部可能没有任何内容。放置一个简单的文本视图或将颜色更改为非常明显的颜色(例如#00F)以确保它已添加。
我正在将 ConstraintLayout 添加到带有工具栏的 LinearLayout。添加 ConstraintLayout 会导致工具栏不再可见,布局检查器显示工具栏可见性 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));
}
content_view.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>
您可能缺少一些东西。
- 图片不完整 - 为什么要动态添加视图?尝试使用
<include layout="@layout/[NAME_OF_THE_VIEW_LAYOUT]"/>
在布局 xml 中添加它并注释掉 java 代码 - 只是为了确定。 - 您正在添加到 LinearLayout,但使用 ViewGroup.LayoutParams,这不可行,请改用 LinearLayout.LayoutParams。
- 这可能与缺少工具栏无关,但ConstraintLayout 内部可能没有任何内容。放置一个简单的文本视图或将颜色更改为非常明显的颜色(例如#00F)以确保它已添加。