以编程方式RelativeLayout.ALIGN_RIGHT与给定的View.getId()无效

时间:2015-02-06 16:28:28

标签: java android relativelayout

我尝试以编程方式将TextView对齐到按钮的右/下边缘。 但是,这个静态xml代码正在运行:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView10"
        android:layout_alignRight="@+id/button"
        android:layout_alignBottom="@+id/button" />
</RelativeLayout>

如果我尝试按代码执行,则无效。 默认情况下,TextView在顶部/左侧对齐。 RelativeLayout.ALIGN_RIGHT和ALIGN_BOTTOM似乎被忽略......

            // Adding the LinearLayout
  LinearLayout linearLayout = new LinearLayout(getContext());
  linearLayout.setOrientation(LinearLayout.VERTICAL);
  linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

            final LinearLayout.LayoutParams LLParamsCont = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, Utils.dpToPx(getContext(), 50));

                    // Adding the RelativeLayout
        RelativeLayout relativeLayout = new RelativeLayout(getContext());
        relativeLayout.setLayoutParams(LLParamsCont);

                    // Adding the Button
        Button button = new Button(getContext());
        button.setLayoutParams(new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        relativeLayout.addView(button)

                    // Adding the TextView
        TextView textView = new TextView(getContext());
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                Utils.dpToPx(getContext(), 20),
                Utils.dpToPx(getContext(), 20));
        layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, button.getId());        // <== THIS DOESN'T SEEM TO WORK
        layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, button.getId());       // <== THIS DOESN'T SEEM TO WORK
        textView.setLayoutParams(layoutParams);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        textView.setGravity(Gravity.CENTER);
        textView.setBackgroundResource(R.drawable.score_count);
        relativeLayout.addView(textView);

        linearLayout.addView(relativeLayout)

TextView不会与Button的右/底行对齐。

有没有人知道为什么不能通过代码工作,而是使用静态xml? 我错过了什么?

谢谢!

最诚挚的问候, 尔根

2 个答案:

答案 0 :(得分:2)

在创建规则之前,尝试为按钮设置唯一ID (更多信息here)。

Button button = new Button(getContext());
button.setId(View.generateViewId()); //this utility method is API 17!

答案 1 :(得分:0)

我在这篇文章中找到了解决方案: How to lay out Views in RelativeLayout programmatically?

我需要将一个带有setId(int)的id设置为Button,然后它才能正常工作。