片段显示在整个屏幕上

时间:2018-07-14 08:41:37

标签: android android-fragments

即使在Dp中设置了FrameLayout的高度和宽度,片段也会显示在整个屏幕上

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>

插入片段之前:

Before Fragment insert

插入片段后:

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();

请,有人知道如何设置这样的片段:

how to set fragment like that

2 个答案:

答案 0 :(得分:0)

如果该片段占据了包括操作栏在内的整个屏幕,那么您将需要添加另一个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)

答案 1 :(得分:0)

您的xml文件可以,只需添加背景颜色即可。

android:background =“#FF69B4”在FrameLayout上

        android:layout_width="300dp"

        android:layout_height="300dp"

        android:background="#FF69B4"
        >

    </FrameLayout>