私はアンドロイドが初めてで、オンラインチャットに関連するアプリケーションを開発しています。チャット画面で、チャット ボックスのサイズをテキスト ビューのサイズに設定しようとしています。
テキスト ビューのサイズの増減に合わせて、チャット ボックスのサイズを増減するにはどうすればよいですか?
これを参照してください
<?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/
Relative Layout
高さwrap_content
と同様に高さを設定しtextview
ます。
WRAP_CONTENT
、つまり、ビューはそのコンテンツを囲むのに十分な大きさにする必要があることを意味します。
できればレイアウトを取り、サイズが増減するRelativeLayout
たびに、 newを動的に設定してチャットビューを更新できます。TextView
LayoutParams