以编程方式 RelativeLayout.ALIGN_RIGHT 给定的 View.getId() 不起作用
Programmatically RelativeLayout.ALIGN_RIGHT with a given View.getId() not working
我尝试以编程方式将 TextView 与按钮的 right/bottom 边缘对齐。
但是,此静态 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 正在对齐 top/left。
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 的 right/bottom 行对齐。
有谁知道为什么这不是通过代码工作,而是通过静态 xml 工作?
我错过了什么?
谢谢!
谨致问候,
于尔根
尝试在创建规则之前为按钮设置一个唯一 ID(更多信息 here)。
Button button = new Button(getContext());
button.setId(View.generateViewId()); //this utility method is API 17!
我在这里 post 找到了解决方案:
How to lay out Views in RelativeLayout programmatically?
我需要用 setId(int) 给按钮设置一个 id,然后它就可以工作了。
我尝试以编程方式将 TextView 与按钮的 right/bottom 边缘对齐。 但是,此静态 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 正在对齐 top/left。 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 的 right/bottom 行对齐。
有谁知道为什么这不是通过代码工作,而是通过静态 xml 工作? 我错过了什么?
谢谢!
谨致问候, 于尔根
尝试在创建规则之前为按钮设置一个唯一 ID(更多信息 here)。
Button button = new Button(getContext());
button.setId(View.generateViewId()); //this utility method is API 17!
我在这里 post 找到了解决方案: How to lay out Views in RelativeLayout programmatically?
我需要用 setId(int) 给按钮设置一个 id,然后它就可以工作了。