ConstraintLayout

时间:2017-10-17 18:07:13

标签: android android-layout android-fragments android-scrollview android-constraintlayout

我正在开发一个多窗格应用程序,这使我使用ConstraintLayout来设计主要布局,因为它看起来很有用。有两个FrameLayouts(左侧和右侧面板)作为碎片容器,顶部有几个工具栏,左侧面板底部有一个BottomNavigationView

我的问题是在右侧面板上使用ScrollView进行片段布局。无论我尝试什么,如果超出可见高度,我都无法滚动布局。通过使用FrameLayout封装片段容器(ScrollView)并根据RecyclerView的测量高度显式设置片段根布局高度,我发现了一种修复此问题的hacky方法在片段中。但是,对于大型列表,这会导致崩溃(Layer exceeds max. dimensions supported by the GPU);而且反正它不是一个理想的解决方案。所以现在,我想改变布局。

我知道不建议RecyclerView内有ScrollView;但是,我在片段布局中有其他元素,所以我需要有这个结构。我错过了这个ConstraintLayout的东西吗?我正在考虑切换到RelativeLayout,因为我相信它会解决这个问题,但我想在做任何重要事情之前先询问社区,因为转换布局需要一些时间。

以下是一些可能有用的详细信息(如果您需要更多详细信息,请与我们联系):

MainActivity布局:

<?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"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/main_activity_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

   <android.support.v7.widget.Toolbar
      android:id="@+id/main_toolbar"
      android:layout_width="0dp"
      android:layout_height="50dp"
      android:theme="@style/CustomToolbar"
      android:background="@drawable/toolbar_background"
      app:contentInsetStart="0dp"
      app:contentInsetEnd="0dp"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintBottom_toTopOf="@+id/main_container"
      app:layout_constraintLeft_toLeftOf="@+id/guideline_cart_right"
      app:layout_constraintRight_toRightOf="parent">

      ......toolbar stuff here......

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

   <FrameLayout
      android:id="@+id/main_container"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:theme="@style/MainFragStyle"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="@+id/guideline_cart_right"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/main_toolbar"/>

      ...... other elements .....

</android.support.constraint.ConstraintLayout>

示例片段布局: (注意:试过玩布局高度属性,没有用)

<ScrollView
   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_height="match_parent"
   android:layout_width="match_parent"
   android:fillViewport="true">
<android.support.constraint.ConstraintLayout
                          android:id="@+id/root_fragment_item"                                                 
                          android:layout_width="match_parent"                                                 
                          android:layout_height="wrap_content"
                          android:focusable="true"
                          android:focusableInTouchMode="true"                                                 
                          android:descendantFocusability="beforeDescendants">

   <View
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:id="@+id/header_section_background"
      android:background="@color/colorItemHeaderBackground"
      app:layout_constraintBottom_toTopOf="@+id/guideline_header_bottom"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"/>

   <View
      android:id="@+id/header_section_triangle"
      android:layout_width="40dp"
      android:layout_height="22dp"
      android:layout_marginLeft="8dp"
      android:layout_marginRight="8dp"
      android:background="@drawable/triangle"
      app:layout_constraintBottom_toBottomOf="@+id/guideline_header_bottom"
      app:layout_constraintHorizontal_bias="0.15"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"/>

   <TextView
      android:id="@+id/header_item_title"
      style="@style/ItemHeaderTitle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="32dp"
      app:layout_constraintBottom_toTopOf="@+id/guideline_header_bottom"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.2"/>

   <TextView
      android:id="@+id/header_item_duration"
      style="@style/ItemHeaderSubtitle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="32dp"
      android:text=""
      app:layout_constraintBottom_toTopOf="@+id/guideline_header_bottom"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.65"/>

   <ImageView
      android:id="@+id/header_item_image"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:background="@drawable/custom_image_background"
      android:layout_margin="8dp"
      app:layout_constraintBottom_toTopOf="@+id/guideline_header_bottom"
      app:layout_constraintLeft_toRightOf="@+id/guideline_header_image_start"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintVertical_bias="0.0"
      app:layout_constraintHorizontal_bias="1.0"/>

   <android.support.v7.widget.RecyclerView
      android:id="@+id/item_details"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:layout_marginBottom="8dp"
      android:layout_marginLeft="8dp"
      android:layout_marginRight="8dp"
      android:layout_marginTop="24dp"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/guideline_header_bottom"/>

   <android.support.constraint.Guideline
      android:id="@+id/guideline_header_bottom"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      app:layout_constraintGuide_begin="172dp"/>

   <android.support.constraint.Guideline
      android:id="@+id/guideline_header_image_start"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.75"/>

</android.support.constraint.ConstraintLayout>
</ScrollView>

0 个答案:

没有答案
相关问题