【问题标题】:How to use GuideLine in ConstraintLayout with Kotlin Anko layout如何在 ConstraintLayout 中使用 GuideLine 和 Kotlin Anko 布局
【发布时间】:2018-03-04 03:26:41
【问题描述】:

问题 1:

虽然我在ConstraintLayout中使用guideline,但它根本不起作用,下面的代码无法设置guidelineorientation(为什么没有这样的属性?),这是没用的:

constraintLayout {            
    guideline {
         //here, cannot set the orientation of guideline
    }
    //...
}

虽然下面的代码可以设置准线方向,但是布局还是有同样的问题(不知道是否创建了准线):

constraintLayout {            
    constraintSet {
         create(ID_GUIDELINE, VERTICAL_GUIDELINE)
    }
    //...
}

那么,在 Anko 的 ConstraintLayout 布局中使用 GuideLines 的正确方法是什么?

问题 2:

另一个ConstraintLayout中的ConstraintLayou在使用Fragment时可能会出现意外行为。

我在Activity中使用ConstraintLayout(以A命名),在Fragment中使用ConstraintLayout(B),然后添加或替换B进入/与A,结果显示B的宽度总是不正确。

但是,如果B中有RecyclerView,则BlayoutParams设置如下:

layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)

那么它就可以正常工作了,没有RecyclerView而只有其他小部件,比如buttons/textviews/imageviews,这表明B的宽度很窄。

当然,我可以将布局 A 更改为 FrameLayout,但这不是我想要的,那么为什么以及如何做到这一点?谢谢回复!

另外,我在这里打开了 Anko kotlin 库中的问题:https://github.com/Kotlin/anko/issues/579,代码:

// A in Activity
constraintLayout {
    fitsSystemWindows = true
    id = A
}
supportFragmentManager.beginTransaction().replace(A, fragmentB).commit()
//supportFragmentManager.beginTransaction().add(A, fragmentB).commit()

// B in Fragment
constraintLayout {
    fitsSystemWindows = true
    layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
}

【问题讨论】:

    标签: android kotlin android-constraintlayout anko


    【解决方案1】:

    问题 1 试试这样的:

        guideline {
          id = ID_GUIDELINE
        }.lparams(0, 0) {
          orientation = ConstraintLayout.LayoutParams.VERTICAL
          guideBegin = dip(80)
        }
    

    问题 2 应该放在单独的帖子中,并且标题要好,这样它更有可能得到回答,并且更容易被其他人搜索。

    【讨论】:

    • 非常感谢,我已经解决了这个问题,只需在RecyclerView的项目视图中设置LayoutParams: ·layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) ·
    猜你喜欢
    • 2023-03-24
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2018-04-17
    • 2021-09-15
    相关资源
    最近更新 更多