我有一个包含 2 个 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 右对齐。但到目前为止,我无法做到这一点。