Android RelativeLayout 最小宽度
Android RelativeLayout minimal width
我正在 Android 中开发一个 Messenger 应用程序,为此我有一个包含文本消息和一些相关信息的 RelativeLayout。我当前的 xml 代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/text_message"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp">
<RelativeLayout
android:id="@+id/chat_message_inner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bubble_a1"
android:padding="1dp">
<TextView
android:id="@+id/text_message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Hello World"
android:textColor="@android:color/black"
android:textSize="@dimen/font_size"/>
<RelativeLayout
android:id="@+id/text_message_info"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_message_text"
android:layout_toEndOf="@id/text_message_text"
android:padding="3dp">
<TextView
android:id="@+id/chat_timeStamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/chat_status"
android:text="00:00"
android:textColor="@color/colorTimeStamp"/>
<ImageView
android:id="@+id/chat_status"
android:layout_width="19dp"
android:layout_height="13dp"
android:layout_alignEnd="@+id/chat_timeStamp"
android:layout_alignRight="@+id/chat_timeStamp"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="6dp"
android:src="@drawable/two_grey_hook"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
小消息看起来不错 (Hello World Message), large messages reaching over multiple lines, however, make the information layout disappear (Longer Message)。
有没有办法避免这个问题?我是否坚持使用 RelativeLayouts 并不重要,它对我来说似乎是最可定制的。
另外,我知道在这个代码提取中似乎可以将两个周围的布局合并为一个,但是,因为我想根据消息的发送者设置布局的引力但不想要背景气泡拉伸整个宽度。
如果您能提供任何帮助,我将不胜感激,如果您愿意,我还可以提供更多代码片段。
感谢 GG15
编辑:
我使用消息的布局只是一个 ListView,然后我扩展了一个 ArrayAdapter 以添加单个消息。我的 getView 函数基本上如下(我把它缩短了一点):
public View getView(int position, View ConvertView, ViewGroup parent){
View v = ConvertView;
MessageArrayContent Obj = getItem(position);
if (Obj.getClass() == TextMessage.class){
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.text_message, parent, false);
TextMessage msgObj = (TextMessage) Obj;
RelativeLayout layoutOuter = (RelativeLayout) v.findViewById(R.id.text_message);
RelativeLayout layoutInner = (RelativeLayout) v.findViewById(R.id.chat_message_inner);
layoutInner.setBackgroundResource(msgObj.left ? R.drawable.bubble_a1 : R.drawable.bubble_b1);
TextView chatText = (TextView) v.findViewById(R.id.text_message_text);
chatText.setText(msgObj.message);
TextView chatTime = (TextView) v.findViewById(R.id.chat_timeStamp);
chatTime.setText(new SimpleDateFormat("HH:mm", Locale.GERMANY).format(msgObj.time));
//here I am doing some irrelevant stuff concerning the message status
}
}else
//I also have some other types of messages, not being relevant here
return v;
}
如果您希望消息保持在特定行内,请尝试使用线性布局。我不确定它是否能满足您的所有需求,因为我不知道您的应用程序的所有要求,但它可能是一个不错的起点。
Linear Layout and weight in Android has some good advice as well as the official android documentation at http://developer.android.com/guide/topics/ui/layout/linear.html
祝你好运。
我正在 Android 中开发一个 Messenger 应用程序,为此我有一个包含文本消息和一些相关信息的 RelativeLayout。我当前的 xml 代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/text_message"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp">
<RelativeLayout
android:id="@+id/chat_message_inner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bubble_a1"
android:padding="1dp">
<TextView
android:id="@+id/text_message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Hello World"
android:textColor="@android:color/black"
android:textSize="@dimen/font_size"/>
<RelativeLayout
android:id="@+id/text_message_info"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_message_text"
android:layout_toEndOf="@id/text_message_text"
android:padding="3dp">
<TextView
android:id="@+id/chat_timeStamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/chat_status"
android:text="00:00"
android:textColor="@color/colorTimeStamp"/>
<ImageView
android:id="@+id/chat_status"
android:layout_width="19dp"
android:layout_height="13dp"
android:layout_alignEnd="@+id/chat_timeStamp"
android:layout_alignRight="@+id/chat_timeStamp"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="6dp"
android:src="@drawable/two_grey_hook"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
小消息看起来不错 (Hello World Message), large messages reaching over multiple lines, however, make the information layout disappear (Longer Message)。 有没有办法避免这个问题?我是否坚持使用 RelativeLayouts 并不重要,它对我来说似乎是最可定制的。
另外,我知道在这个代码提取中似乎可以将两个周围的布局合并为一个,但是,因为我想根据消息的发送者设置布局的引力但不想要背景气泡拉伸整个宽度。
如果您能提供任何帮助,我将不胜感激,如果您愿意,我还可以提供更多代码片段。
感谢 GG15
编辑:
我使用消息的布局只是一个 ListView,然后我扩展了一个 ArrayAdapter 以添加单个消息。我的 getView 函数基本上如下(我把它缩短了一点):
public View getView(int position, View ConvertView, ViewGroup parent){
View v = ConvertView;
MessageArrayContent Obj = getItem(position);
if (Obj.getClass() == TextMessage.class){
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.text_message, parent, false);
TextMessage msgObj = (TextMessage) Obj;
RelativeLayout layoutOuter = (RelativeLayout) v.findViewById(R.id.text_message);
RelativeLayout layoutInner = (RelativeLayout) v.findViewById(R.id.chat_message_inner);
layoutInner.setBackgroundResource(msgObj.left ? R.drawable.bubble_a1 : R.drawable.bubble_b1);
TextView chatText = (TextView) v.findViewById(R.id.text_message_text);
chatText.setText(msgObj.message);
TextView chatTime = (TextView) v.findViewById(R.id.chat_timeStamp);
chatTime.setText(new SimpleDateFormat("HH:mm", Locale.GERMANY).format(msgObj.time));
//here I am doing some irrelevant stuff concerning the message status
}
}else
//I also have some other types of messages, not being relevant here
return v;
}
如果您希望消息保持在特定行内,请尝试使用线性布局。我不确定它是否能满足您的所有需求,因为我不知道您的应用程序的所有要求,但它可能是一个不错的起点。
Linear Layout and weight in Android has some good advice as well as the official android documentation at http://developer.android.com/guide/topics/ui/layout/linear.html
祝你好运。