【问题标题】:RelativeLayout params AddRule doesn't consistently work?RelativeLayout 参数 AddRule 不能始终如一地工作?
【发布时间】:2013-01-11 15:12:40
【问题描述】:

我有一个 RelativeLayout 的子视图,我正在尝试修改 Java 中的布局参数。我得到LayoutParams 如下:

RelativeLayout.LayoutParams dlp = (LayoutParams) dimBackground.getLayoutParams();

dlp.addRule(RelativeLayout.BELOW, currentTextView.getId());
//dlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

dimBackground.setLayoutParams(dlp);

dlp.addRule(RelativeLayout.BELOW, currentTextView.getId()); 行不会改变dimBackground 的位置,但是如果我取消注释下面的行,那么dimBackground 将与父视图的底部对齐。

知道为什么第一条规则不起作用吗?

编辑:

按照建议,我在 xml 中进行了尝试,但仍然没有运气。下面是我的xml:

<RelativeLayout
    android:id="@+id/dim_background_bottom"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_below="@+id/product_subtotal_row"
    android:background="@color/gray_tint"
    android:visibility="visible" />

【问题讨论】:

  • 2 cmets: 1) 这行dimBackground.setLayoutParams(dlp); 是不必要的,dlp 已经是当前的背景布局,你不必重新设置。尝试在 XML 上手动创建此布局(仅用于测试),可能存在某些现有规则与您添加的新规则冲突。
  • @Budius : dimBackground.setLayoutParams(dlp); 将调用 requestLayout() 所以它是必需的。
  • 你打电话给requestLayout(); =]
  • 您需要验证您的 currentTextView 是否有一个 Id(即使它是通过编程方式或在 XML 中完成的)。
  • 它确实有一个id(我已经在代码中验证过)

标签: android android-layout android-relativelayout


【解决方案1】:

(由于您没有发布太多有关布局的详细信息……很难给您明确的答案)。

我猜你的currentTextView 和你的dimBackground 没有相同的父 relativeLayout。 正如RelativeLayout 文档中提到的那样:

一种布局,其中子级的位置可以相对于彼此或与父级进行描述。

所以请仔细检查currentTextViewdimBackground 是同一个RelativeLayout 的孩子

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 2015-07-12
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多