0

相対レイアウトがどのように機能するかを理解しようとしています。LinearLayout を使用する場合、2 つの編集テキスト フィールドが横に並んで表示されることはわかっています。'weight' を使用すると、画面サイズのみに基づいて edittext フィールドのサイズが変更されます。(おそらく奇妙に見えるでしょうが、それはうまくいきます)。

linearlayout が最も効率的ではないことはわかっています。相対レイアウトを使用して同じ効果を得ることができますか? サイズを指定したくありません。編集テキストを展開したいだけです。

このような線形レイアウトでは、xml はどのように見えるでしょうか?

ありがとう、マイク

4

4 に答える 4

1

ほとんどのAndroid開発者が人生を費やしているのを私が見ることができるものの1つはレイアウトです。

私がRelativeLayoutsでやりたいことを実行できるようですが、少なくとも2の倍数に対してのみです。少なくともこれまでに見つけたものです。ボタンの中央では、2つのボタンが同じ行に配置され、それぞれが画面の約半分を占めます。ボタンの中央がないと、それらは互いに重なります。それは価値があるのだろうか。

これがお役に立てば幸いです。

<View android:id="@+id/button_center" android:layout_width="0dp"
    android:layout_height="0dp" android:layout_centerHorizontal="true" />

<Button android:id="@+id/btnButton1" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="Button 1" 
    android:layout_toLeftOf="@+id/button_center"/>

<Button android:id="@+id/btnButton2" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="Button 2"
    android:layout_toRightOf="@+id/button_center" />

于 2012-11-19T23:43:06.017 に答える
0

重みはパーセンテージではないことに注意してください。重みは、元のサイズに応じて重みを付けます。ウェイトで目的を達成するには、LinearLayout に android:weightSum を追加するだけです。

http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="1.0" >

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Left" >
    </EditText>

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Right" />
</LinearLayout>
于 2012-11-19T23:56:48.607 に答える
0

これは alignLeft/alignRight で実行できると思っていましたが、残念ながら入れ子になった 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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" >

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Left" >

            <requestFocus />
        </EditText>

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="Right" />
    </LinearLayout>

</RelativeLayout>
于 2012-11-19T15:01:37.183 に答える
0

EditText フィールドで width="fill_parent" と height="fill_parent" を使用した場合はどうなるでしょうか (パディングやマージンを使用することもできます)。

于 2012-11-19T15:04:56.423 に答える