Android线性布局中文本视图的编程右对齐

3

我正在处理动态UI。我有一个父LinearLayout,在其中创建了另一个LinearLayout,并在其中添加了TextView。我希望将该TextView右对齐到屏幕。

我尝试了以下方法:

public MessageView(LinearLayout parent, Message msg) {
    myParent = parent;

    messageDetails = new LinearLayout(parent.getContext());
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);

    layoutParams.gravity = RelativeLayout.ALIGN_PARENT_RIGHT;

    messageDetails.setLayoutParams(layoutParams);

    senderTV = new TextView(parent.getContext());
    senderTV.setTypeface(null, Typeface.ITALIC);
    messageDetails.addView(senderTV);
}

我该如何做到这一点?

1个回答

3

试试这个:

messageDetails = new RelativeLayout(parent.getContext());
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
messageDetails.addView(senderTV);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接