Android - 以编程方式在布局中定位TextView

时间:2016-08-16 21:03:30

标签: android android-layout android-imageview textview android-relativelayout

我不知道如何在RelativeLayout的右侧设置TextView。将重力添加到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

1 个答案:

答案 0 :(得分: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);

现在您可以使用此代码:)