我正在尝试将一些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;
    }

最佳答案

尝试添加以下规则。

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


另外,您可能应该删除

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


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

10-08 06:59