ConstraintLayout使单元格项适合屏幕视口

时间:2017-11-13 17:21:57

标签: android xml android-layout android-view android-constraintlayout

我正在尝试创建一个看起来像这样的屏幕

但是当我按照下面的

使用XML代码创建视图时
<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:id="@+id/fb_constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="me.buddy.buddy.ui.BenefitsActivity$BenefitsFragment">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/fb_animals"
        android:src="@drawable/animal_love"

        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/fb_meal_data"
        app:layout_constraintBottom_toTopOf="@id/fb_environment"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/fb_meal_data"
        android:src="@drawable/food"

        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/fb_animals"
        app:layout_constraintBottom_toTopOf="@id/fb_health"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/fb_environment"
        android:src="@drawable/environment"

        app:layout_constraintTop_toBottomOf="@id/fb_animals"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/fb_health"
        app:layout_constraintBottom_toTopOf="@id/fb_hunger"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/fb_health"
        android:src="@drawable/health"

        app:layout_constraintTop_toBottomOf="@id/fb_meal_data"
        app:layout_constraintBottom_toTopOf="@id/fb_section_label"
        app:layout_constraintLeft_toRightOf="@id/fb_environment"
        app:layout_constraintRight_toRightOf="parent"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/fb_hunger"
        android:src="@drawable/bowl"

        app:layout_constraintTop_toBottomOf="@id/fb_environment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/fb_section_label"/>



    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/fb_section_label"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"


        app:layout_constraintTop_toBottomOf="@id/fb_health"
        app:layout_constraintLeft_toRightOf="@id/fb_hunger"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

我在我的应用中获得了如下屏幕

请注意,此布局位于活动的片段内,如xml中的tools标记所示。因此,实际布局中的附加浮动操作按钮和后退箭头。

然而,我无法理解的是为什么食物碗的图标在实际布局中被推到可见屏幕空间下方。

对此问题的任何解决方案?我想尽量避免使用Scrollview。

供参考,父活动的XML代码附于

之下
    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="me.buddy.buddy.ui.BenefitsActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/ab_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/ab_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="@string/app_name">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/ab_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/ab_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_menu_share" />
</android.support.design.widget.CoordinatorLayout>

用于将片段添加到主要活动的Java代码,如下所示:

首先是父类:

setContentView(R.layout.activity_benefits);
Toolbar toolbar = (Toolbar) findViewById(R.id.ab_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.ab_container);
mViewPager.setAdapter(mSectionsPagerAdapter);

然后在Fragment类中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_benefits_dashboard, container, false);
return rootView;}

2 个答案:

答案 0 :(得分:2)

您尝试解决的问题可以通过<input type="button" value="play" onclick="play()"> <input type="button" value="play2" onclick="play2()"> Guideline轻松完成。

您只需定义2条横向指南:一条为33%,另一条为66%。 然后,您所要做的就是根据这些指导方针确定您的观点。

这是屏幕的骨架实现:

ConstraintLayout

这是输出:

答案 1 :(得分:2)

主要活动中的Toolbar推送ViewPager。 AppBarLayout主要用于选项卡式布局以使用TabLayout。如果您在TabLayout内使用AppBarLayout,则Toolbar会在启动活动后自动隐藏,ViewPager将占据整个窗口。

解决方案: 从您的活动中删除整个AppBarLayout Toolbar,并从活动清单中删除NoActionBar主题

或者,不要将CoordinatorLayout用于您的父活动。

相关问题