承载viewPager的Android FrameLayout会占用所有可用空间
大家好,我正在尝试创建一个“教程”(就像这个来自AirBnb应用程序的教程),其中包含不同的图像和文本(都托管在片段中),并由FrameLayout中的(片段)ViewPager管理。 正如上面的例子,我还想在屏幕底部显示两个按钮,我的问题是,包含ViewPager的FrameLayout会影响所有可用空间,从而隐藏包含按钮的LinearLayout 这是一个奇怪的情况,因为布局非常简单,我尝试了不同的方法,但没有解决我的问题。我检查了布局的根元素是否处于填充父模式,并检查了所有其他布局是否避免增长超过显示其信息所需的空间 以下是FragmentViewPager显示的活动和每个子片段的一些片段和xml布局: activity-tutorial.xml(我尝试将LinearLayout作为根元素而不是RelativeLayout,但没有任何变化) 一切正常,显示当前页面的点也正常,但按钮根本不显示。这是我的应用程序结果:承载viewPager的Android FrameLayout会占用所有可用空间,android,android-layout,android-fragments,android-viewpager,android-framelayout,Android,Android Layout,Android Fragments,Android Viewpager,Android Framelayout,大家好,我正在尝试创建一个“教程”(就像这个来自AirBnb应用程序的教程),其中包含不同的图像和文本(都托管在片段中),并由FrameLayout中的(片段)ViewPager管理。 正如上面的例子,我还想在屏幕底部显示两个按钮,我的问题是,包含ViewPager的FrameLayout会影响所有可用空间,从而隐藏包含按钮的LinearLayout 这是一个奇怪的情况,因为布局非常简单,我尝试了不同的方法,但没有解决我的问题。我检查了布局的根元素是否处于填充父模式,并检查了所有其他布局是否避免
谢谢。您需要更改布局的行为。首先,对齐父对象底部的
线性布局。然后将您的FrameLayout
放在LinearLayout
上方。大概是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/pager_framelayout"
android:layout_width="fill_parent"
android:layout_above="@+id/buttons_bottom_layout"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/tutorial_pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/circle_indicator"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@null"
android:layout_gravity="bottom" />
</FrameLayout>
<LinearLayout
android:id="@+id/buttons_bottom_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="100"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/sign_up_btn"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="50"
android:text="Sign UP" />
<Button
android:id="@+id/login_btn"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="50"
android:text="Login" />
</LinearLayout>
</RelativeLayout>
我不得不使用android:layout\u alignParentBottom=“true”,但它工作得很好!令人惊叹的!谢谢你知道为什么会发生这种情况吗?有没有在线资源可以解释这种情况?再次感谢。是的,当然,应该是android:layout\u alightParentBottom=“true”
,我错了。您的ViewPager
已全屏显示,因此底部按钮位于ViewPager
下方,这意味着它们不在屏幕上。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/tutorial_screen_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/tutorial_screen_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/general_padding"
android:background="@null"
android:layout_gravity="bottom"/>
</FrameLayout>
{ //...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Identify and set fields!
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.subfragment_tutorial_screen, container, false);
image = (ImageView) rootView.findViewById(R.id.tutorial_screen_image);
title = (TextView) rootView.findViewById(R.id.tutorial_screen_title);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate fields with info!
if (TutorialActivity.class.isInstance(getActivity())) {
title.setText(titleResId);
// Call Glide to load image
Glide.with(this)
.load(imageResId)
.centerCrop()
.placeholder(R.drawable.image_placeholder)
.into(image);
}
}
/...
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/pager_framelayout"
android:layout_width="fill_parent"
android:layout_above="@+id/buttons_bottom_layout"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/tutorial_pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/circle_indicator"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@null"
android:layout_gravity="bottom" />
</FrameLayout>
<LinearLayout
android:id="@+id/buttons_bottom_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="100"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/sign_up_btn"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="50"
android:text="Sign UP" />
<Button
android:id="@+id/login_btn"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="50"
android:text="Login" />
</LinearLayout>
</RelativeLayout>