android - ConstraintLayout 使单元格项适合屏幕视口(viewport)

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

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

但是当我按照下面的方式使用 XML 代码创建 View 时

<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>

我的应用程序中出现如下所示的屏幕

请注意,此布局位于 Activity 的 fragment 内,如 xml 中的工具标记所示。因此,实际布局中会出现额外的 float 操作按钮和后退箭头。

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

这个问题有什么解决办法吗?我想尽可能避免使用 Scrollview。

作为引用,父 Activity 的 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>

将 fragment 添加到主要 Activity 的 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;}

最佳答案

您尝试解决的问题可以使用 ConstraintLayout 轻松完成 Guideline

您只需定义 2 个水平准则:一个在 33%,另一个在 66%。 然后,您所要做的就是根据这些准则来定位您的观点。

这是屏幕的框架实现:

<?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"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#53d94f"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintEnd_toStartOf="@+id/view2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#d9c74f"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/view1"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#4f68d9"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toStartOf="@+id/view4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/guideline" />

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#d95a4f"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/view3"
        app:layout_constraintTop_toTopOf="@id/guideline" />

    <View
        android:id="@+id/view5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#4fd4d9"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view6"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline2" />

    <View
        android:id="@+id/view6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#c94fd9"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/view5"
        app:layout_constraintTop_toBottomOf="@+id/guideline2" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.33" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.66" />
</android.support.constraint.ConstraintLayout>

这是输出:

关于android - ConstraintLayout 使单元格项适合屏幕视口(viewport),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47270032/

相关文章:

java - 无法使用 GAPID 在 Android 模拟器上启动跟踪

Android - 使用数据绑定(bind)的条件文本值

javascript - 如何用javascript读取这个xml文件?

ruby - 如何在 Ruby 中使用 Nokogiri 迭代 XML 嵌套元素

Python:写入 xml 文件时丢失内容

android - AutoCompleteTextView 将选项限制为适配器中设置的选项,并且在列表可见之前不需要两个字符

android - 在运行时创建的 Robolectric 测试中加载文件/ Assets

android - 房间 : replace SQLite cursor in Dao?

android - fragment 内容被切断

Android - 如何对齐 TextView 文本