2

アプリの XML レイアウトを作成していて、ボタンをテキストの下に配置する必要がありましたが、LinearLayout を使用して一部のテキストをプロパティの重みで水平方向に配置したため、ボタンは別のレイアウトのままで、プロパティを定義するようになりました。 android:layout_below" で、テキストの ID が見つかりません。では、Java を使用せずにこの問題を解決できる提案はありますか?

これが私のコードです:

<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" >

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3" >

<TextView
    android:id="@+id/text1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="text1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/text2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="text2"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/text3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="text3"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/text1"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/text1"
    android:gravity="center"
    android:text="Button" />

</RelativeLayout>

編集: ボタンは に揃える必要がありますtext1。アイデアは、align_left、align_right、および align_below の 3 つのプロパティを使用して、ボタンのテキストの幅が同じになるようにすることです。そして、テキストでプロパティを設定するための他のビューがあります。

4

1 に答える 1

0

LinearLayout の id を設定できます。

<LinearLayout 
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3" >

を使用してレイアウトの下にボタンを配置します

android:layout_below="@+id/text"

ボタンを最初のテキストの下に配置するには、text1 とボタンを垂直方向の線形レイアウトで囲むことができます。

<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="wrap_content" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:weightSum="3" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="text1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Button" />

</LinearLayout>

   <TextView
      android:id="@+id/text2"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:gravity="center"
      android:text="text2"
      android:textAppearance="?android:attr/textAppearanceLarge" />

   <TextView
      android:id="@+id/text3"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:gravity="center"
      android:text="text3"
      android:textAppearance="?android:attr/textAppearanceLarge" />

   </LinearLayout>

  </RelativeLayout>
于 2012-10-19T23:46:10.963 に答える