3

content をラップするように相対レイアウトを設定し、すべての子を右に揃えます。ここでは xml です。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout18"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000">


    <TextView
        android:id="@+id/textViewMsgDate"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="right"
        android:layout_marginRight="8dp"
        android:gravity="center"
        android:textColor="#ffff"
        android:text="simple"
        />

</RelativeLayout>

ここに画像の説明を入力

しかし、あなたが写真でわかるように、私は成功しませんか? ただし、左に揃えるとwrap_contentは期待どおりに機能します。

4

1 に答える 1

2

ここでの問題は、RelativeLayout の幅が wrap_content に設定されているため、RelativeLayout の子をその親 (RelativeLayout) の右に揃えていることです。これは子を移動しません。

子を親の右に揃えたくない場合は、親の幅を子より大きくする必要があります。したがって、RelativeLayout に幅を与えるか、「match_parent」にするか、RelativeLayout 自体を右に揃えて、子 (TextView) が一緒に移動するようにします。

注 :左に揃えて RelativeLayout の幅を「wrap_content」にすると、期待どおりに動作します。RelativeLayout は既に左に揃えられているため、子も同様です。

編集済み

あなたが望むものを達成するには、これを行うだけです:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout18"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout
    android:id="@+id/textview_wrapper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="#000">

    <TextView
       android:id="@+id/textViewMsgDate"
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:layout_marginRight="8dp"
       android:textColor="#ffff"
       android:text="simple"
    />
</RelativeLayout></RelativeLayout>

結果は次のようになります。

出力

于 2015-09-09T17:29:37.487 に答える