Android TextView右对齐LinearLayout以编程方式对齐

时间:2015-09-14 17:56:11

标签: android android-layout

我正在研究动态UI。我有Parent LinearLayout,我在其中创建了另一个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 个答案:

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