2

私はこのようなレイアウトを持っています:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="@drawable/bgr">
    <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true"
            >
        <TextView android:layout_width="60dp" android:layout_height="wrap_content"
                  android:id="@+id/label"
                  style="@style/ConnectionPoint.Label"
                  android:text="Title"
                />
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                  android:id="@+id/code"
                  style="@style/ConnectionPoint.Code"
                  android:text="CODE"
                />
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                  android:id="@+id/name"
                  android:layout_toRightOf="@id/label"
                  android:layout_toLeftOf="@id/code"
                  style="@style/ConnectionPoint.Name"
                  android:text="Some text"
                />
    </RelativeLayout>
</RelativeLayout>

そしてスタイル:

<style name="ConnectionPoint.Text" parent="android:TextAppearance">
    <item name="android:layout_alignParentBottom">true</item>
</style>
<style name="ConnectionPoint.Label" parent="ConnectionPoint.Text">
    <item name="android:textColor">@color/from_to</item>
    <item name="android:layout_marginLeft">5dp</item>
    <item name="android:layout_marginRight">10dp</item>
    <item name="android:textSize">16dp</item>
</style>
<style name="ConnectionPoint.Name" parent="ConnectionPoint.Text">
    <item name="android:textSize">26dp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:singleLine">true</item>
    <item name="android:ellipsize">end</item>
</style>
<style name="ConnectionPoint.Code" parent="ConnectionPoint.Text">
    <item name="android:textColor">@color/iata</item>
    <item name="android:textSize">18dp</item>
    <item name="android:singleLine">true</item>
    <item name="android:layout_alignParentRight">true</item>
    <item name="android:layout_marginRight">5dp</item>
    <item name="android:layout_marginLeft">5dp</item>
</style>

しかし、結果の TextViews は適切に配置されていません。

ここに画像の説明を入力

それらを下に揃えたいのですが、なぜ RelativeLayout でこれができないのか理解できません。RelativeLayout に TextView を揃えさせるにはどうすればよいですか?

4

4 に答える 4

6

テキストビューを選択してプロパティに移動

select Center Vertical for each textview

また

put android:layout_centerVertical="true" in xml

于 2012-09-24T10:33:05.707 に答える
3

このコードを試してください:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true" >

    <TextView
        android:id="@+id/label"
        style="@style/ConnectionPoint.Label"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Title" />

    <TextView
        android:id="@+id/code"
        style="@style/ConnectionPoint.Code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="CODE" />

    <TextView
        android:id="@+id/name"
        style="@style/ConnectionPoint.Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Some text" />
  </RelativeLayout>

  </RelativeLayout>
于 2012-09-24T10:37:07.357 に答える
0

この質問は2年前のものであることは知っていますが、他の誰かがこれに遭遇した場合、より良い答えが得られたと思います. ネストされた相対レイアウトでテキストビューを下に揃えようとする私の問題を解決しました。

OPのコードを仮定すると:

<RelativeLayout 
        android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true">
<TextView 
        android:layout_width="60dp" android:layout_height="wrap_content"
        android:id="@+id/label"
        style="@style/ConnectionPoint.Label"
        android:text="Title"/>
<TextView 
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:id="@+id/code"
        style="@style/ConnectionPoint.Code"
        android:text="CODE"/>
<TextView 
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:id="@+id/name"
        android:layout_toRightOf="@id/label"
        android:layout_toLeftOf="@id/code"
        style="@style/ConnectionPoint.Name"
        android:text="Some text"/>

以下は、これらの要素を下に揃えるために機能するはずです。

<RelativeLayout
          android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true">
        <TextView
                android:layout_width="60dp" android:layout_height="wrap_content"
                android:id="@+id/label"
                android:text="Title"/>
        <TextView
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:id="@+id/code"
                android:layout_alignBottom="@+id/label"
                android:text="CODE"/>
        <TextView
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:id="@+id/name"
                android:layout_toRightOf="@id/label"
                android:layout_toLeftOf="@id/code"
                android:layout_alignBottom="@+id/label"
                android:text="Some text"/>
    </RelativeLayout>

ここでの唯一の変更は、次の行の追加です。

android:layout_alignBottom="@+id/{your-id-here}"

レイアウトに基づいて微調整する必要がある場合があります。上記の例では、id=label を持つ TextView が本質的にルートであり、他の要素はそこから底を取得します。そのため、id=label には layout_alignBottom プロパティがありません。

于 2015-01-16T18:03:43.323 に答える
0

残念ながら、唯一の実用的な解決策は、ネストされた RelativeLayout の代わりに LinearLayout を使用しないことです。

于 2012-09-25T08:36:27.833 に答える