【问题标题】:Is it good idea to use Constrain layout inside Scrollview in Android?在 Android 的 Scrollview 中使用约束布局是个好主意吗?
【发布时间】:2019-10-11 14:42:29
【问题描述】:

我作为 OEM 开发人员工作,我们为所有应用程序使用通用 GUI 库。在我们的应用程序中,我们扩展了带有 ScrollView 作为我的应用程序基本布局的 GUI 库。现在我的团队正计划在应用程序中使用约束布局。我们可以在 Scroll View 中使用约束布局吗?

我使用 Android 设计工具将线性布局转换为约束布局。但 滚动不起作用。

<ScrollView
    style="@style/Body_ScrollView"
    android:id="@+id/no_sim_layout">

    <LinearLayout style="@style/Body.LinearLayout.No_Sim">
        <-- All other child views are going here-->
    </LinearLayout>

</ScrollView>

想转换成

<ScrollView
    style="@style/Body_ScrollView"
    android:id="@+id/no_sim_layout">

    <ConstrainLayout style="@style/Body.LinearLayout.No_Sim">
           <-- All other child views are going here-->
    </ConstrainLayout>

</ScrollView>

【问题讨论】:

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


    【解决方案1】:

    是的,肯定是 Constraint-layout 首先明白这一点:

    ConstraintLayout 的意图是通过对每个视图应用一些规则来优化和扁平化布局的视图层次结构,以避免嵌套。推荐使用相对布局规则。

    更多——它提供动态视图对齐属性作为约束、基线、视图链接和许多其他提供无缝扁平层次结构的东西。如果我们在 Scrollview 中使用了 Constraint,我们就不需要像在 LinearLayout 中的称重和许多其他的那样管理每个视图属性。它更简单,直接从提供的依赖中获得好处。

    <ConstrintLayout>
       <ScrollView>
        <ConstrintLayout>
            //Single ParentConstrain else child
        </ConstrintLayout>
       </ScrollView>
    </ConstrintLayout>
    

    如果采用其他方式,这会使您的复杂 XML 代码更难理解,并且基于在机器上运行时计算的权重和位置构建 UI 会很繁重。

    约束直接受益于输入依赖关系

     <ConstrintLayout>
           <ScrollView>
            <LinearLayout>
                //Many different view to manage view
            </LinearLayout>
           </ScrollView>
        </ConstrintLayout>
    

    【讨论】:

      猜你喜欢
      • 2017-06-05
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      相关资源
      最近更新 更多