繁体   English   中英

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

[英]Android TextView Right Align in LinearLayout Programmatically

我正在研究动态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);
}

我该怎么做?

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM