RelativeLayout params AddRule不能始终如一地工作?

时间:2013-01-11 15:12:41

标签: android android-layout relativelayout

我有一个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" />

1 个答案:

答案 0 :(得分:2)

(由于你没有发布太多关于你的布局的细节......很难给你一个明确的答案)。

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

  

一种布局,其中儿童的位置可以相互描述或与父母相关。

因此请仔细检查currentTextViewdimBackground是否属于同一个RelativeLayout的孩子