繁体   English   中英

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 ),大消息跨越多行,但是,信息布局消失了( 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;
  }

如果希望邮件保留在特定行内,请尝试使用线性布局。 我不确定它是否能满足您的所有需求,因为我不了解您的应用程序的所有要求,但它可能是一个不错的起点。

Android中的线性布局和权重以及http://developer.android.com/guide/topics/ui/layout/linear.html上的官方android文档都提供了一些很好的建议

祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM