【问题标题】:How can I set negative percent margins in PercentRelativeLayout?如何在 PercentRelativeLayout 中设置负百分比边距?
【发布时间】:2016-01-10 23:17:09
【问题描述】:

我需要在 PercentRelativeLayout 中设置负边距,但是当我在 xml 中尝试如下时它不起作用。

app:layout_marginTopPercent="-10%"

我尝试在测量父级时以编程方式设置负边距,但 PercentRelativeLayout 保留其子级 LayoutParams 并恢复对它们所做的任何更改。

在 dp 中设置负边距的工作方式如下,但我绝对需要提供相对于父尺寸的负边距。

android:layout_margin="-10dp"

我该怎么做,有什么想法吗?

【问题讨论】:

    标签: android android-layout android-percentrelativelayout android-percent-library


    【解决方案1】:

    在测量父级时,我最终根据父级大小将视图转换为负值。这不是一个可靠的解决方案,但它适用于我的情况。

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        float marginTop = h / -10.0f;
        child.setTranslationY(marginTop);
    }
    

    【讨论】:

      【解决方案2】:

      PercentRelativeLayout 使用 -1.0f 作为默认边距值 并且助手只设置一个值 >= 0.0f 在您的情况下没有必要使用此布局; 您可以将 marginTopPercent 设置为 -1.0f 并自行使用 MarginLayoutParams 将值放入 java 中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-12
        • 2016-09-05
        相关资源
        最近更新 更多