gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:53:19 26 4
gpt4 key购买 nike

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

关于Android TextView以编程方式在LinearLayout中右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32570937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com