Textview 没有粘在屏幕的右边缘 (Android/Java)

Textview not sticking to the right edge of the screen (Android/Java)

我试图让一些 textview 贴在屏幕的右侧(并将其右对齐)。我想以编程方式执行此操作,但找不到合适的方法。

我尝试使用 setGravity 和 setTextAlignment。但是 none 似乎有效。

answershort.setGravity(Gravity.RIGHT);
answershort.setTextAlignment(TEXT_ALIGNMENT_VIEW_END)

知道我做错了什么吗?

public void createanswershort(String key){
        answershort = new TextView(getApplicationContext());
        RelativeLayout.LayoutParams av = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, // Width of TextView
                RelativeLayout.LayoutParams.WRAP_CONTENT); // Height of TextView
        av.addRule(RelativeLayout.BELOW, laatstetxtview.getId());
        av.addRule(RelativeLayout.RIGHT_OF, laatstetxtviewsmall.getId());
        av.setMargins(35,10,25,0);
        answershort.setLayoutParams(av);
        int sID = Tools.generateViewId();
        answershort.setId(sID);
        answershort.setGravity(Gravity.RIGHT);
        answershort.setTextAlignment(TEXT_ALIGNMENT_VIEW_END);
        answershort.setText(Html.fromHtml(key).toString());
        answershort.setTextSize(15);
        answershort.setTextColor(Color.parseColor("#000000"));
        answershort.setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));
        rl.addView(answershort);
        laatstetxtview = ts;
    }

尝试添加以下规则。

av.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

此外,您或许应该删除

av.addRule(RelativeLayout.RIGHT_OF, laatstetxtviewsmall.getId());

您正在尝试将视图对齐到屏幕右侧,而不是彼此对齐。