Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式将RelativeLayout.ALIGN_与给定视图向右对齐。getId()不起作用_Java_Android_Android Relativelayout - Fatal编程技术网

Java 以编程方式将RelativeLayout.ALIGN_与给定视图向右对齐。getId()不起作用

Java 以编程方式将RelativeLayout.ALIGN_与给定视图向右对齐。getId()不起作用,java,android,android-relativelayout,Java,Android,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_con

我尝试以编程方式将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)
//添加线性布局
LinearLayout LinearLayout=新的LinearLayout(getContext());
linearLayout.setOrientation(linearLayout.VERTICAL);
linearLayout.setLayoutParams(新的linearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_父项,LinearLayout.LayoutParams.WRAP_内容,1.0f));
最终LinearLayout.LayoutParams LLParamsCont=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,Utils.dpToPx(getContext(),50));
//添加RelativeLayout
RelativeLayout RelativeLayout=新的RelativeLayout(getContext());
relativeLayout.setLayoutParams(LLParamsCont);
//添加按钮
按钮按钮=新按钮(getContext());
button.setLayoutParams(新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容);
relativeLayout.addView(按钮)
//添加文本视图
TextView TextView=newtextView(getContext());
textView.setGravity(重力。水平中心);
RelativeLayout.LayoutParams LayoutParams=新的RelativeLayout.LayoutParams(
Utils.dpToPx(getContext(),20),
dpToPx(getContext(),20));

layoutParams.addRule(RelativeLayout.ALIGN_RIGHT,button.getId());// 在创建规则(更多信息)之前,尝试为按钮设置唯一id


我在这篇文章中找到了解决方案:


我需要在按钮上设置一个带有setId(int)的id,然后它就可以工作了。

hello,如果删除这两行中的“+”,xml还能工作吗?android:layout_alignRight=“@+id/button”android:layout_alignBottom=“@+id/button””谢谢,刚刚在同一秒发布。但是你是对的,我需要给按钮设置一个id。
Button button = new Button(getContext());
button.setId(View.generateViewId()); //this utility method is API 17!