【问题标题】:How can I bring view to top in LinearLayout without change index of the view?如何在不更改视图索引的情况下在 LinearLayout 中将视图置于顶部?
【发布时间】:2016-09-11 02:12:15
【问题描述】:

例如,这里是 LinearLayout 中的 6 个子视图。

  • 线性布局
    • 查看0
    • 查看1
    • 查看2
    • 查看3
    • 查看4
    • 查看5

在某些情况下,我想让 View0 在其他视图之上,也就是说,View0 不能被其他视图重叠。

bringToFrontbringChildToFront这两个方法,它们会将View0的索引改为:

  • 线性布局
    • 查看1
    • 查看2
    • 查看3
    • 查看4
    • 查看5
    • 查看0

我试过ViewCompat.setTranslationZ的方法,但是不行。

如何在不更改 View0 索引的情况下将 View0 置于其他视图之上?

【问题讨论】:

  • 如果您查看源代码,您会发现 ViewCompat 方法实际上在旧 SDK 级别上没有任何作用。
  • @AutonomousApps 是的,我应该注意到这一点。

标签: android android-linearlayout


【解决方案1】:

您需要继承 LinearLayout,使用 setChildDrawingOrderEnabled(true),并覆盖 getChildDrawingOrder() 以最后返回项目的索引。

因此,在您的示例中,您将在 getChildDrawingOrder(int childCount, int i) 中返回以下内容:

return (childCount + (i-1)) % childCount;

【讨论】:

  • use setChildDrawingOrderEnabled(true)我觉得这个方法应该是setChildrenDrawingOrderEnabled,但是,还是不行……
  • 我试试return childCount - i - 1;,我工作了,谢谢!
【解决方案2】:

尝试将 View0 的布局重力设置为 Gravity.TOP

【讨论】:

    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多