簡體   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