gpt4 book ai didi

android - 右对齐 ListView 项目中的 2 个 TextView

转载 作者:行者123 更新时间:2023-11-29 18:08:37 25 4
gpt4 key购买 nike

我有一个包含 2 个 TextView 的 ListView 。一个 TextView 显示在气泡背景中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- android:background="#FFDBE2ED"-->
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<RelativeLayout
android:id="@+id/wrapper2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<!-- android:layout_gravity="center" -->
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"


android:layout_marginTop="5dip"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"
android:background="@drawable/bubble_yellow"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="@android:color/primary_text_light" />
<TextView
android:id="@+id/sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_margin="0dip"
android:background="#00dcdcdc"
android:paddingLeft="10dip"
android:text="Hello bubbles!"

android:textColor="#b9dcdcdc"
android:layout_below="@+id/comment"
/>


</RelativeLayout>

</LinearLayout>


</LinearLayout>

有时我在右边显示textview,有时在左边

public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
}

wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

OneMessage coment = getItem(position);

countryName = (TextView) row.findViewById(R.id.comment);

countryName.setText(coment.msgText);
countryName.setGravity(!coment.sentbyuser ? Gravity.LEFT : Gravity.RIGHT) ;

sender = (TextView) row.findViewById(R.id.sender);
sender.setGravity(Gravity.LEFT) ;

countryName.setBackgroundResource(!coment.sentbyuser ? R.drawable.bubble_yellow : R.drawable.bubble_green);
wrapper.setGravity(!coment.sentbyuser ? Gravity.LEFT : Gravity.RIGHT);

return row;
}

当我在左侧显示 2 个 TextView 时,一切正常。但是,当我在右侧显示 2 textview 时,我希望 2 Textview 右对齐。但到目前为止,我无法做到这一点。

最佳答案

TextView.setGravity() 定义了 subview 的定位方式,参见 docs .您实际上想要对齐 textView 而不是它的子项。您将要使用方法 setLayoutParams() 参见 docs .我不确定您要使用 wrapper LayoutView 做什么。所以下面的代码可能不是您需要的全部。但这是将您的 View 与相对布局对齐(实用地)的推荐方法。

public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
}

wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

OneMessage coment = getItem(position);

countryName = (TextView) row.findViewById(R.id.comment);

countryName.setText(coment.msgText);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

if(!coment.sentbyuser)
{
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
else
{
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}

countryName.setLayoutParams(params);

return row;
}

如果有第二个 View 需要与 countryName 完全对齐,您可以使用以下内容:

View yourOtherView = findViewById(R.id.yourOtherView);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_BELOW, R.id.comment);
params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.comment);

yourOtherView.setLayoutParams(params);

这告诉它在评论 View 下方对齐,然后右对齐。您可能需要尝试调整边距、填充和其他值才能使其完全正确。

祝你好运!

关于android - 右对齐 ListView 项目中的 2 个 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12042664/

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