2013-01-10 71 views
0

我看到一些問題可以與此類似。但是,這些情況有所不同,有些不適合初學者。
所以我剛剛開始Android。請理解我是*.xml的新手。無論如何,我關心的是嵌套的LinearLayout只顯示第一個View嵌套的LinearLayout只顯示第一個視圖

<LinearLayout 
    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" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

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

     <TextView 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:layout_width="0dp" 
      android:textSize="20sp" 
      android:text="@string/app_title"> 
     </TextView> 

    </LinearLayout> 

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

     <Button 
      android:layout_gravity="center" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="@string/button_ok" 
      android:onClick="changeMessage"> 
     </Button> 

     <TextView 
      android:layout_gravity="center" 
      android:textSize="20sp" 
      android:id="@+id/this_text" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="@string/sample_text"> 
     </TextView> 

    </LinearLayout> 

</LinearLayout> 

到目前爲止,這裏是XML。有了這段代碼,只有第一個TextView只顯示。

回答

1

android:layout_height="match_parent"嵌套LinearLayout的高度應wrap_content而不是match_parent

+0

感謝您的反饋意見。我會在重新啓動我的電腦後嘗試。 –

+0

無論如何,這不是一項任務。爲了我的學習,如果我可能會問,將layout_height設置爲match_parent後發生了什麼?我在做Java,所以我認爲ViewGroup的功能就像一個JPanel。 –

+0

與match_parent視圖(LineraLayout)將作爲其父項高。有了wrap_content,它會很高,因爲它的內容 – Blackbelt

1

,因爲你給了第一LinearLayout寬度和高度matchparent

更改其高度wrapcontent

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

    <TextView 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:textSize="20sp" 
     android:text="@string/app_title"> 
    </TextView> 

</LinearLayout> 

// other layouts 
1

改變佈局如下

<LinearLayout 
    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" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="match_parent" 
    android:layout_width="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:textSize="20sp" 
     android:text="@string/app_title"> 
    </TextView> 

</LinearLayout> 

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

    <Button 
     android:layout_gravity="center" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="@string/button_ok" 
     android:onClick="changeMessage"> 
    </Button> 

    <TextView 
     android:layout_gravity="center" 
     android:textSize="20sp" 
     android:id="@+id/this_text" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="@string/sample_text"> 
    </TextView> 

</LinearLayout> 

你的內佈局與父佈局因此只有第一佈局將示出匹配。

相關問題