右对齐ListView项目中的2个TextView

时间:2012-08-20 18:01:09

标签: android

我有一个包含2个Textview的列表视图。一个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个文本视图时,一切都很好。但是,当我在右侧显示2个textview时,我希望2 Textview是正确对齐的。但到目前为止,我无法做到这一点。

1 个答案:

答案 0 :(得分:3)

TextView.setGravity()定义了如何查看子视图,请参阅docs。您希望实际对齐textView而不是其子项。您将要使用setLayoutParams()方法,请参阅docs。我不确定你要用wrapper LayoutView做什么。因此,以下代码可能不是您需要的所有内容。但建议您将视图与相对布局对齐(实际)。

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

如果有第二个视图需要与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);

告诉它将它对齐在你的评论视图下方,然后右对齐它。您可能需要使用边距,填充和其他值来完全正确。

祝你好运!