Android 为什么单个片段在活动中占据整个屏幕,而其他视图使用Constraint布局?

Android 为什么单个片段在活动中占据整个屏幕,而其他视图使用Constraint布局?,android,android-layout,android-fragments,android-constraintlayout,Android,Android Layout,Android Fragments,Android Constraintlayout,在我的主要活动中有两个框架布局,在运行时将被两个片段替换。我正在使用约束布局来对齐它们。 一个特定的片段占据了id为recentsfragment的framelayout,它正在替换整个屏幕,而没有给其他framelayout留出任何位置 当我单独显示框架布局及其片段时,视图与预期完全一致 活动\u main.xml <layout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas

在我的主要活动中有两个框架布局,在运行时将被两个片段替换。我正在使用约束布局来对齐它们。 一个特定的片段占据了id为recentsfragment的framelayout,它正在替换整个屏幕,而没有给其他framelayout留出任何位置

当我单独显示框架布局及其片段时,视图与预期完全一致

活动\u main.xml

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
>
 <androidx.constraintlayout.widget.ConstraintLayout


  android:layout_width="match_parent"
  android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/recentsfragment"
    android:layout_width="395dp"
    android:layout_height="325dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.618"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view"></FrameLayout>

<view
    android:id="@+id/view"
    class="android.widget.Button"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginBottom="8dp"
    android:background="@android:color/darker_gray"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="-51dp" />

<FrameLayout
    android:id="@+id/allnotesfragment"
    android:layout_width="395dp"
    android:layout_height="327dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toTopOf="@+id/view"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"></FrameLayout>

我为他们尝试了不同的路线,但没有成功。我特别想使用约束布局。 谢谢你的帮助

试试这个xml:

<androidx.constraintlayout.widget.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="wrap_content">


<FrameLayout
    android:id="@+id/allnotesfragment"
    android:layout_width="395dp"
    android:layout_height="327dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toTopOf="@+id/view"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"></FrameLayout>

<view
    android:id="@+id/view"
    class="android.widget.Button"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="@android:color/darker_gray"
    app:layout_constraintBottom_toTopOf="@id/recentsfragment"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/allnotesfragment" />

<FrameLayout
    android:id="@+id/recentsfragment"
    android:layout_width="395dp"
    android:layout_height="325dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/view"></FrameLayout>

试试这个xml:

<androidx.constraintlayout.widget.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="wrap_content">


<FrameLayout
    android:id="@+id/allnotesfragment"
    android:layout_width="395dp"
    android:layout_height="327dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toTopOf="@+id/view"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"></FrameLayout>

<view
    android:id="@+id/view"
    class="android.widget.Button"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="@android:color/darker_gray"
    app:layout_constraintBottom_toTopOf="@id/recentsfragment"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/allnotesfragment" />

<FrameLayout
    android:id="@+id/recentsfragment"
    android:layout_width="395dp"
    android:layout_height="325dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/view"></FrameLayout>


试试这种线性布局:

<LinearLayout 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="wrap_content"
android:orientation="vertical">

<FrameLayout
    android:id="@+id/recentsfragment"
    android:layout_width="395dp"
    android:layout_height="325dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="15dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp" >

</FrameLayout>

<view
    android:id="@+id/view"
    class="android.widget.Button"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:background="@android:color/darker_gray" />

<FrameLayout
    android:id="@+id/allnotesfragment"
    android:layout_width="395dp"
    android:layout_height="327dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp" >

</FrameLayout>

</LinearLayout>
以及onCreate中的以下代码:

    recentFragment = findViewById(R.id.recentsfragment);
    allNotesFragment = findViewById(R.id.allnotesfragment);

    recentFragment.requestLayout();
    allNotesFragment.requestLayout();

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (getHeight()/2)-10);
    recentFragment.setLayoutParams(layoutParams);
    allNotesFragment.setLayoutParams(layoutParams);

    addFragment();

尝试以下线性布局:

<LinearLayout 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="wrap_content"
android:orientation="vertical">

<FrameLayout
    android:id="@+id/recentsfragment"
    android:layout_width="395dp"
    android:layout_height="325dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="15dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp" >

</FrameLayout>

<view
    android:id="@+id/view"
    class="android.widget.Button"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:background="@android:color/darker_gray" />

<FrameLayout
    android:id="@+id/allnotesfragment"
    android:layout_width="395dp"
    android:layout_height="327dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp" >

</FrameLayout>

</LinearLayout>
以及onCreate中的以下代码:

    recentFragment = findViewById(R.id.recentsfragment);
    allNotesFragment = findViewById(R.id.allnotesfragment);

    recentFragment.requestLayout();
    allNotesFragment.requestLayout();

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (getHeight()/2)-10);
    recentFragment.setLayoutParams(layoutParams);
    allNotesFragment.setLayoutParams(layoutParams);

    addFragment();
检查您的约束。您给出的顶部和底部约束是错误的。 试试下面的代码


检查您的约束。您给出的顶部和底部约束是错误的。 试试下面的代码



您只需将两个框架布局更改为
0dp
类似于
android:layout\u height=“0dp”
您只需将两个框架布局更改为
0dp
类似于
android:layou height=“0dp”
您可以使用
app:layout\u constraint\u height=“0.5”
用于两种
框架布局
并实现2个视图,占据一半屏幕大小,甚至不需要xml中的oyur
视图
——如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">


<FrameLayout
    android:id="@+id/recentsfragmentTop"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHeight_percent=".5"
    app:layout_constraintBottom_toTopOf="@+id/allnotesfragmentBottom"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@color/cardview_dark_background" />



<FrameLayout
    android:id="@+id/allnotesfragmentBottom"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHeight_percent=".5"
    app:layout_constraintTop_toBottomOf="@+id/recentsfragmentTop"
    android:background="@color/colorAccent"
    tools:ignore="RtlHardcoded" />
</androidx.constraintlayout.widget.ConstraintLayout>


您可以对两个
框架布局使用
app:layout\u constraintHeight\u percent=“.5”
,并实现两个视图,它们占据了屏幕大小的一半,甚至不需要在xml中显示您的
视图,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">


<FrameLayout
    android:id="@+id/recentsfragmentTop"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHeight_percent=".5"
    app:layout_constraintBottom_toTopOf="@+id/allnotesfragmentBottom"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@color/cardview_dark_background" />



<FrameLayout
    android:id="@+id/allnotesfragmentBottom"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHeight_percent=".5"
    app:layout_constraintTop_toBottomOf="@+id/recentsfragmentTop"
    android:background="@color/colorAccent"
    tools:ignore="RtlHardcoded" />
</androidx.constraintlayout.widget.ConstraintLayout>



问题的真正原因不是布局,而是因为在上面的一个片段中,我不适当地使用了数据绑定,它占据了应用程序的整个屏幕,没有为其他片段留出空间。谢谢

这个问题并不是因为布局,而是因为在上面的一个片段中,我使用了不适当的数据绑定,它占据了应用程序的整个屏幕,没有为其他片段留下任何空间。感谢您

尝试更改两个
FrameLayout
标记以匹配约束,
android:layout\u width=“0dp”
android:layout\u height=“0dp”
感谢您的响应,但这并没有解决问题。尝试更改两个
FrameLayout
标记以匹配约束,
android:layout\u width=“0dp”
android:layout\u height=“0dp”
感谢您的回复,但这并没有解决问题。它也不起作用。另一个片段膨胀整个屏幕,你可以考虑使用另一种布局,比如线性布局,如果是的话,我有一个XML供你尝试。我添加了线性布局,当我尝试从主活动初始化第二个片段时,我得到一个错误:“对于片段,ID没有找到任何视图”。好,这里是“重写保护空格”。(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RecentsFragment RecentsFragment=new RecentsFragment();BlankFragment BlankFragment=new BlankFragment();fragmentManager=getSupportFragmentManager();fragmentManager.beginTransaction()。添加(R.id.recentsfragment,recentsfragment).commit();fragmentManager.beginTransaction().add(R.id.allnotesfragment,blankFragment).commit()另一个片段会膨胀整个屏幕,你可以考虑使用另一种布局,比如线性布局,如果是的话,我有一个XML供你尝试。我添加了线性布局,当我尝试从主活动初始化第二个片段时,我得到一个错误:“对于片段,ID没有找到任何视图”。.okay,这里是@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RecentsFragment RecentsFragment=new RecentsFragment();BlankFragment BlankFragment=new BlankFragment();fragmentManager=getSupportFragmentManager();fragmentManager.beginTransaction().add(R.id.recentsfragment,recentsfragment.commit();fragmentManager.beginTransaction().add(R.id.allnotesfragment,blankFragment).commit();这并没有解决问题。现在显示新错误。**“找不到id 0x7f070044(com.example.diary:id/allnotesfragment blankFragment)的视图”**好的,请尝试编辑您的问题,然后附加错误的屏幕截图,或者按照问题中的方式包含代码,因为这两种方法在这里都很好,我认为问题出在代码的其他地方。这并没有解决问题。现在新的错误出现了。**“找不到id 0x7f070044的视图(com.example.diary:id/allnotesfragment)对于片段BlankFragment“**好的,试着编辑你的问题,然后附上错误的屏幕截图或包含问题中的代码,因为这两种方法在这里都很好,我认为问题在代码中的其他地方。答案很好,你得到了我的支持。检查我下面的答案,我相信它更简单。此外,它可以改变高度(响应所有屏幕大小)在几秒钟内完成两种框架布局。也许你可以从我的答案中学到一些新的东西。每次你学到一些新的东西,你就会成为一个更好的开发人员伟大的答案,你得到了我的支持。检查下面我的答案,我相信它更简单。此外,它可以改变高度(响应所有屏幕大小)在几秒钟内完成两种框架布局。也许你可以从我的答案中学到一些新的东西。每次你学到一些新的东西,你都可以