所以我在将对话用片段添加到活动时遇到了问题。该片段是使用
添加的常规片段supportFragmentManager.beginTransaction().add(...).commit()
问题是片段已添加到活动中,但是活动的工具栏和浮动操作按钮将显示在片段的顶部。我知道这是因为这两个视图已提升到4dp,并且可以通过将片段高度更改为<4dp来解决。我的问题是,有更好的方法吗?是否可以在活动顶部添加片段?还是这是必须完成的方式?
活动
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:elevation="4dp"
android:gravity="center"
android:text="@string/app_name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="24dp"/>
对话框片段
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:background="#40000000">
<FrameLayout
android:elevation="4dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:background="@color/white"
android:layout_height="250dp"/>
答案 0 :(得分:0)
活动开始时,尝试使用可见性来隐藏视图,这是最简单的方法,floatingActionButton.setVisibility(View.GONE);
答案 1 :(得分:0)
为了隐藏floatActionButton,请在fragment
onCreateView
中添加以下内容:
//Global Variable
private View view;
@Override
public View onCreateView(@NonNull LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup group = (ViewGroup) view.getParent();
if (group != null) {
group.removeView(view);
}
} else {
floatingActionButton = view.findViewById(R.id. floatingActionButton);
floatingActionButton.hide();
...
return view;
}
在您的xml中:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
//If this is your toolbar, why are you instantiating a textview and not a toolbar???
<TextView
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:elevation="4dp"
android:gravity="center"
android:text="@string/app_name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
app:layout_constraintTop_toTopOf="parent" />
// get rid off below toolbar so that the container will cover the whole height and width of the screen
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="24dp"/>
答案 2 :(得分:0)
提供的答案更多的是hack,而不是解决方案。当用户在片段之间来回导航时,隐藏,显示,重新隐藏和重新显示是一种痛苦。更好的解决方案是在其中包含ConstraintLayout的情况下使用RelativeLayout(如果同时使用多个片段显示)或LinearLayout / FrameLayout父级。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:elevation="4dp"
android:gravity="center"
android:text="@string/app_name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="24dp"/>
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
不要忘记添加您的ID,例如rl_container,用作片段的容器的引用。干杯!