即使在Dp中设置了FrameLayout的高宽,Fragment也会全屏显示
minSdkVersion 16
targetSdkVersion 27
activity_main2.xml
<RelativeLayout 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=".Main2Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:padding="8dp"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end" />
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="300dp"
android:layout_height="300dp">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="26dp"
android:clickable="true"
android:src="@drawable/ic_menu_send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/></RelativeLayout>
fragment 插入前:
fragment 插入后:
fragment_home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/opacity" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="home" /></LinearLayout>
MainActivity.java
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, new HomeFragment(),HomeFragment.HOME_FRAGMENT_TAG)
.addToBackStack("Home")
.commit();
请问,有没有人知道如何设置这样的 fragment :
最佳答案
如果 fragment 占据整个屏幕,包括操作栏,那么您需要添加另一个 xml 名称 content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.digiclack.videostatus.MainActivity"
tools:showIn="@layout/app_bar_main">
<FrameLayout
android:id="@+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
然后在 app_bar_main.xml 中删除 FrameLayout 标签并添加
<include layout="@layout/content_main" />
现在这样引用
.replace(R.id.frag_container, new HomeFragment(),HomeFragment.HOME_FRAGMENT_TAG)
关于android - fragment 全屏显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51336798/