1

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

4

1 回答 1

3

TextView.setGravity()正在定义如何对子视图进行定位,请参阅文档。您想要实际对齐 textView 而不是它的子项。您将要使用该方法,setLayoutParams()请参阅docs。我不确定您要对wrapperLayoutView 做什么。所以下面的代码可能不是你需要的一切。但这是将视图与相对布局对齐的推荐方法(务实地)。

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

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

祝你好运!

于 2012-08-20T18:18:13.170 回答