0

私はアンドロイドが初めてで、オンラインチャットに関連するアプリケーションを開発しています。チャット画面で、チャット ボックスのサイズをテキスト ビューのサイズに設定しようとしています。

テキスト ビューのサイズの増減に合わせて、チャット ボックスのサイズを増減するにはどうすればよいですか?

4

3 に答える 3

1

これを参照してください

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:weightSum="1.0"
android:layout_weight="1" android:layout_height="wrap_content">

<TextView android:id="@+id/lefttext" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dip" 
    android:layout_marginRight="10dip" 
    android:layout_marginTop="10dip"
    android:layout_marginBottom="5dip"
    android:layout_alignParentLeft="true"
    android:maxWidth="250dip"/>

<TextView 
    android:id="@+id/righttext" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip" 
    android:layout_marginRight="10dip" 
    android:layout_marginTop="10dip"
    android:layout_marginBottom="5dip"
    android:layout_alignParentRight="true"
    android:maxWidth="250dip"/>
  </RelativeLayout>

これは、カスタム配列アダプターのgetViewメソッド内のコードです。

View view = convertView;
if(view == null){
     view = mInflater.inflate(R.layout.list_item, null);
}

Resources res = getContext().getResources();
Drawable bubblesChat = res.getDrawable(R.drawable.bubbles_chat);
Drawable bubblesResponse = res.getDrawable(R.drawable.bubbles_response);
TextView left = (TextView) view.findViewById(R.id.lefttext);
TextView right = (TextView) view.findViewById(R.id.righttext);

String txt = super.getItem(position);
if(txt.startsWith("s:")) {
    left.setText(getItem(position));
    left.setBackgroundDrawable(bubblesChat);
    right.setText("");
    right.setBackgroundDrawable(null);
} else {
    right.setText(getItem(position));
    right.setBackgroundDrawable(bubblesResponse);
    left.setText("");
    left.setBackgroundDrawable(null);
}
return view;

代替のチャットバブル幅http://warting.se/2012/06/04/chat-bubbles-in-android/

于 2012-09-24T09:08:58.400 に答える
1

Relative Layout高さwrap_contentと同様に高さを設定しtextviewます。

WRAP_CONTENT、つまり、ビューはそのコンテンツを囲むのに十分な大きさにする必要があることを意味します。

于 2012-09-24T09:05:06.943 に答える
0

できればレイアウトを取り、サイズが増減するRelativeLayoutたびに、 newを動的に設定してチャットビューを更新できます。TextViewLayoutParams

于 2012-09-24T09:25:05.817 に答える