0

これに似た xml レイアウトを作成したかったのですが、xml コードがうまくいきませんでした。

ここに画像の説明を入力

これが私のxmlファイルです:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_margin="5dp"
    android:layout_weight="1"
    android:background="#FFFFFFFF" >

    <ImageButton
        android:name = "@+id/switchOriginDest"
        android:src="@drawable/switchorigdest"
        android:layout_height = "fill_parent"
        android:layout_width = "wrap_content"
        android:layout_alignParentLeft="true" />

    <TextView
        android:id="@+id/Origin"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/rounded_edge"
        android:padding="6dip"
        android:hint="@string/mycurrentpos" />

    <TextView
        android:id="@+id/Destination"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/rounded_edge"
        android:layout_below="@id/Origin"
        android:hint="@string/desthint"
        android:padding="6dip" />

</RelativeLayout>

私が今持っているのは

ここに画像の説明を入力

- - - - - アップデート - - - - - -

TextView の layout_width を固定数に設定すると、問題が解決しました。しかし、誰かが私からのフォローアップの質問に親切に答えてもらえますか?

  1. コンポーネントが残りのスペースを自動的に占めるようにする0dpではありません。この場合、どうしてうまくいかなかったのでしょう。

  2. 例に示すように、赤と緑の円を作成するにはどうすればよいですか?

4

2 に答える 2

0

これを試してください(必要に応じて変更してください):

線形レイアウトの使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:weightSum="1"
    android:orientation="horizontal" >

    <ImageButton
        android:name="@+id/switchOriginDest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/back_arrow" />
   <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
       >
       <TextView
        android:id="@+id/Origin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="6dip"
        android:textSize="30sp"
        android:hint="mycurrentpos" />

    <TextView
        android:id="@+id/Destination"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="desthint"
        android:textSize="30sp"
        android:padding="6dip" />
   </LinearLayout>
</LinearLayout>

それが役立つことを願っています。

相対レイアウトの使用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/switchOriginDest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/back_arrow" />

    <TextView
        android:id="@+id/Origin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/switchOriginDest"
        android:hint="mycurrentpos"
        android:padding="6dip"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/Destination"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Origin"
        android:layout_toRightOf="@+id/switchOriginDest"
        android:hint="desthint"
        android:padding="6dip"
        android:textSize="30sp" />

</RelativeLayout>
于 2013-10-30T08:53:44.783 に答える
0

あなたの例では、layout_width set do 0dp は明確ではありません。ウェイトを使用してサイズを管理する場合は、設定することをお勧めしますが、レイアウト サイズにはウェイトを設定しません。

wrap_content またはいくつかの固定サイズを使用してみて、それが役立つかどうかを確認してください:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="0dp" //here it seems ok, since you set the weight to 1
    android:layout_margin="5dp"
    android:layout_weight="1"
    android:background="#FFFFFFFF" >

    <ImageButton
        android:name = "@+id/switchOriginDest"
        android:src="@drawable/switchorigdest"
        android:layout_height = "fill_parent"
        android:layout_width = "50dp"  //since you src image is bigger than what you need to display, you can't rely on wrap content here ;)
        android:layout_alignParentLeft="true" />

    <TextView
        android:id="@+id/Origin"
        android:layout_width="200dp"  //lets try with this size.
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/rounded_edge"
        android:padding="6dip"
        android:hint="@string/mycurrentpos" />

    <TextView
        android:id="@+id/Destination"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" //you probably want to align that on the bottom ?
        android:background="@drawable/rounded_edge"
        android:layout_below="@id/Origin"
        android:hint="@string/desthint"
        android:padding="6dip" />

</RelativeLayout>

0dp サイズの使用に関する詳細については、こちらをご覧ください。

于 2013-10-30T08:53:59.647 に答える