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

时间:2018-10-14 20:24:43

标签: java android textview text-alignment

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

我尝试使用setGravity和setTextAlignment。但是似乎没有任何作用。

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;
    }

1 个答案:

答案 0 :(得分:2)

尝试添加以下规则。

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

此外,您可能应该删除

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

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