3

任意のテキストを含む TextView を含む動的サイズの RelativeLayout があります。また、タイムスタンプ付きの固定サイズの TextView があります。RelativeLayout と TextView の両方をタイムスタンプとともに 1 行に並べて配置したいと考えています。

このような:

手に入れたいもの

私が本当に得ているもの:

私が本当に得ているもの

ご覧のとおり、テキストを含むレイアウトは幅全体を占めています。

XML:

    <RelativeLayout android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/news_list_element"
                android:layout_marginRight="5dip"
                android:layout_alignParentLeft="true"
                android:focusable="false"
                android:paddingRight="8dip"
                android:paddingLeft="17dip"
                android:paddingTop="4dip"
                android:paddingBottom="4dip">
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/news_list_text"
              android:background="@android:color/transparent"
              android:textColor="#000000"
              android:focusable="false"
              android:padding="3dip"
            />
</RelativeLayout>
<TextView android:id="@+id/messages_dialog_timestamp_in"
          android:background="#000000"
          android:text="12:05"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_toRightOf="@id/news_list_element"
          android:paddingBottom="9dip"
          android:paddingLeft="0dip"
          android:textColor="#8f9eac"
        />
4

1 に答える 1

1

これを試して:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:id="@+id/news_list_text" 
        android:background="@android:color/transparent" 
        android:textColor="#000000" 
        android:focusable="false" 
        android:padding="3dip" 
        android:layout_weight="1"
        android:layout_gravity="left"
    /> 
    <TextView android:id="@+id/messages_dialog_timestamp_in" 
        android:background="#000000" 
        android:text="12:05" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingBottom="9dip" 
        android:paddingLeft="0dip" 
        android:textColor="#8f9eac" 
        android:layout_gravity="right"
    /> 

これandroid:layout_gravity="left"により、メッセージ TextView が親の左端から開始されます。

android:layout_gravity="right"、タイムスタンプ Textview を親の右端に配置します。

をメッセージのTextView とandroid:layout_width="0dp"組み合わせると、他のすべてのウィジェットが配置され、必要なスペースが確保された 後、残りのスペースをすべて埋めるように拡張されます (したがって、タイムスタンプが画面から押し出されることはありません)。android:layout_weight="1"

このレイアウトは、短いメッセージでもテキストビュー領域が拡張されて利用可能なスペースを埋め、タイムスタンプがすべて右端の垂直線に表示されるため、表示されるものとは少し異なります (IMO ではよりきれいに見えます)。

于 2012-06-14T15:25:12.650 に答える