1

2つのTextviewを含むリストビューがあります。1つのテキストビューがバブルの背景の中に表示されます。

<?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>

テキストビューを右側に表示することもあれば、左側に表示することもあります

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つのテキストビューを表示すると、2つのテキストビューが右揃えになると思います。しかし、これまでのところ、私はそれを達成することができませんでした。

4

1 に答える 1

3

TextView.setGravity()子ビューがどのように配置されるかを定義しています。ドキュメントを参照してください。子ではなく、実際にtextViewを整列させたいとします。この方法を使用する場合は、ドキュメントsetLayoutParams()を参照してください。LayoutViewで何をしようとしているのかわかりません。したがって、次のコードは必要なものすべてではない可能性があります。ただし、ビューを相対的なレイアウトに(実用的に)揃えるのに推奨される方法です。wrapper

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と正確に位置合わせする必要がある2番目のビューがある場合は、次を使用できます。

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 に答える