0

我不知道如何在RelativeLayout的右侧设置TextView。向TextView添加重力是行不通的。感谢您的帮助。Android - 以编程方式在布局中定位TextView

RelativeLayout relativeLayout = new RelativeLayout(context); 
    relativeLayout.setGravity(Gravity.CENTER_HORIZONTAL); // center in linearLayout_power_stations_bar 

    textView = new TextView(context); 
    textView.setText(String.valueOf(nuke_count)); 
    textView.setTextColor(activity.getResources().getColor(R.color.game_powerStationsBar_textOverIcon)); 
    textView.setTextSize(16); 

    relativeLayout.addView(textView); // TextView over image 
    relativeLayout.addView(imageView); // adding image 

    linearLayout_power_stations_bar.addView(relativeLayout); // adding to other layout 
+0

有没有你正在做此布局在Java中,而不是在XML理由吗? –

回答

0

您可以将布局参数设置为TextView并设置父权对齐。

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
textViewsetLayoutParams(params); 

现在你可以使用这个代码:)

+0

您能否提供更多详细信息,如代码示例或文档链接? –

+0

现在我根据您的要求更新我的答案:) –

+0

啊,现在我理解我的问题了。你的代码工作正常,谢谢,但在我的情况是,父级布局的父母有一个巨大的宽度和ALIGN_PARENT_RIGHT它将设置TextView到第一个父级布局(FrameLayout)。我有FrameLayout(巨大宽度) - > LinearLayout(垂直方向) - > RelativeLayout(内容文本和img) - > TextView和ImageView。 现在TextView设置在FrameLayout的右侧,但它应该设置在Linear或RelativeLayout的右侧,所以TextView在ImageView上。 – Ghork

相关问题