Android layout 如何实现以下布局?
在我的Android应用程序中,我有一个活动,它将以下组件包装在ConstraintLayout all width=match_parent,height=wrap_content中: 我希望title和adUnit保持固定,其余部分垂直滚动。我或多或少地做到了这一点,但片段不再显示其所有内容——我认为它们都被设置为相同的高度。我如何在这里实现我想要的 以下是我当前的布局,仅涉及相关部分:Android layout 如何实现以下布局?,android-layout,android-constraintlayout,android-scrollview,Android Layout,Android Constraintlayout,Android Scrollview,在我的Android应用程序中,我有一个活动,它将以下组件包装在ConstraintLayout all width=match_parent,height=wrap_content中: 我希望title和adUnit保持固定,其余部分垂直滚动。我或多或少地做到了这一点,但片段不再显示其所有内容——我认为它们都被设置为相同的高度。我如何在这里实现我想要的 以下是我当前的布局,仅涉及相关部分: <android.support.constraint.ConstraintLayout
<android.support.constraint.ConstraintLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v4.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/adView"
app:layout_constraintTop_toBottomOf="@id/tv_title">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<fragment
android:id="@+id/fragment_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
tools:layout="@layout/fragment_1" />
<fragment
android:id="@+id/fragment_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/fragment_1"
tools:layout="@layout/fragment_2" />
<fragment
android:id="@+id/fragment_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/fragment_2"
tools:layout="@layout/fragment_3" />
<fragment
android:id="@+id/fragment_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/fragment_3"
tools:layout="@layout/fragment_4" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
<include
android:id="@+id/adView"
layout="@layout/layout_ad_view"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
您必须创建这种类型的布局。检查下面的xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Title" />
<android.support.v4.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize=""
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
我会先发布你当前的布局文件,这样更多的人可以回答你的问题。谢谢,我现在已经添加了。谢谢。框架布局使我的所有片段显示在彼此的顶部,就像它们都固定在顶部一样。我该怎么处理呢?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="Title" />
<android.support.v4.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize=""
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_container, new Fragment1()); //Fragment2(),Fragment3(),Fragment4()
ft.commit();