1

私の macbook ではエミュレーターの実行速度が非常に遅いため、エミュレーターではなく、Nexus 7 デバイスにコンパイルして実行しています。

これが私のコードです:

<?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="fill_parent"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/hello" />

   <Button 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/button_send"/>

</LinearLayout>

私は本に従っていますが、何が間違っているのかわかりません。ADT Eclipse ide を使用しています。何か案は?テキストはありますが、ボタンが表示されません。グラフィカル ビューにボタンを追加すると、コードを挿入すると機能するように見えます。

4

2 に答える 2

2

LinearLayoutは、fill_parentを使用するときに、各子に必要なサイズを1つずつ測定します。したがって、あなたの例では、最初の子が、与えられたすべての利用可能な高さ(fill_parent)を持ちたいと考えていることがわかります。

間隔を気にしない場合

android:layout_height="fill_parent"に変更android:layout_height="wrap_content"

下部にボタンが必要だと仮定します

これは、layout_weightを使用して修正できます。両方のビューに、必要なスペースのみを使用するように指示します(つまり、wrap_contentを使用します)。次に、layout_weight = 1を使用するように最初のビューを設定します。これにより、残りのスペースを取得するビューを決定するときに、最初のビューが優先されます。

<?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="fill_parent"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Hello" />

   <Button 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_weight="0"
       android:text="Send"/>

</LinearLayout>

結果は次のようになります。これは、あなたが望むと私が想定していることです。

ここに画像の説明を入力してください

于 2013-02-02T19:34:35.630 に答える
1

あなたの TextView は親を埋めます。そのため、ボタン用のスペースが残っていません。変化する:

android:layout_height="fill_parent"

に:

android:layout_height="wrap_content"
于 2013-02-02T19:29:46.133 に答える