android - fragment 全屏显示

标签 android android-fragments

即使在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 插入前:

Before Fragment insert

fragment 插入后:

After Fragment insert

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 :

how to set fragment like that

最佳答案

如果 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/

相关文章:

java - TextInputEditText 不适用于 Android 5.1

android - File.renameTo() 返回 true,但文件尚未重命名

android - 单击后隐藏/显示 ListView 项目中的元素

android - 如何获取 Android FragmentPagerAdapter 的 fragment 引用

php - Android转PHP加密代码

java - 为什么 Recyclerview 列表在所有位置返回相同的项目?

android - 使用 keytool 构建的每个 .keystore 文件是否都是唯一的?

android - ScrollView 内 TextView 的空指针异常

安卓 : Call a Fragment Method from a service

android - 如何添加动态 fragment 标签项