2011-06-20 21 views
1

「私がしましたのLinearLayout問題(同じXMLで2 LLayout)

...私は、Androidアプリをdesingingための新しいXMLのフィルスをコーディングしていると私は同じXMLに2 linearLayourを使用して、いくつかの問題をしましたエラーがXMLファイルに次のコードで「ビルド中止:

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView android:id="@+id/texte_firsttab" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"/> 

</LinearLayout> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 


     <Button android:id="@+id/accessGraphe" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="test" 
    android:onClick="selfDestruct" /> 

    <Button android:id="@+id/accessGraphe2" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="test2" 
    android:onClick="selfDestruct" /> 
</LinearLayout> 

私たちは、同じXMLファイルに2つの線形レイアウトを置くことはできますか?

答えて

1

トップレベルレイアウトを2つ設定することはできません。システムはどのようにそれらを手配するかを知っていますか?これを定義する別のレイアウトにそれらを囲む必要があります。

<?xml ...タグの前の空白は、あなたの投稿のコードの書式化によるものであり、実際のレイアウトファイルには存在しないものとします。それはまた問題を引き起こすでしょう。

+0

あなたは正しいです;-)ありがとうございます!それはそれのように良いです! – clement

1

いいえ。あなたはソースコードからどうやって参照してください。

あなたが同じ時間(上に1と下部に1つずつ)で2つの線形レイアウトを持つようにしたい場合は、あなたが別のレイアウト内のものを埋め込む必要があります。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weigth="1"> 

    <TextView android:id="@+id/texte_firsttab" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"/> 

</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weigth="1" > 


     <Button android:id="@+id/accessGraphe" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="test" 
    android:onClick="selfDestruct" /> 

    <Button android:id="@+id/accessGraphe2" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="test2" 
    android:onClick="selfDestruct" /> 
</LinearLayout> 
</LinearLayout> 
0

次の2つのルートレベルを持っています(これはAndroidのレイアウトファイルに固有のものではなく、XMLファイルには1つのドキュメント要素のみを持つことができます)。

LinearLayoutsを別のものにラップすることはお勧めできません。それは複雑すぎます。一般的にレイアウトのネストを避けることをお勧めします。効率的なレイアウトについてはthis articleを参照してください。

TextViewと2つのボタンでは、RelativeLayoutが完璧です。 LLよりも柔軟性があります。

関連する問題