2015-09-25 26 views
1

佈局高度0dp我要實現以下目標:的Android使用兩個孩子和家長

enter image description here

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical">  

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.42"> 

     <ListView 
      android:id="@+id/ist" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" <!-- GETTING ERROR : Suspicious size: this will make the view invisible, probably intended for layout_width --> 
      android:layout_weight="1" 
      android:background="@color/Bkgd" 
      android:transcriptMode="alwaysScroll" 
      android:stackFromBottom="true" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:gravity="center_vertical" 
      android:background="@android:color/white" > 

      <Button android:id="@+id/Btn"     
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/input_btn" 
       android:background="@android:color/white" /> 

     </LinearLayout> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.58" 
     android:background="@color/colorPrimary" > 

     <GridView 
      android:id="@+id/gridview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="5dp" 
      android:columnWidth="30dp" 
      android:numColumns="auto_fit" 
      android:horizontalSpacing="10dp" 
      android:stretchMode="columnWidth" 
      android:gravity="center" /> 

    </LinearLayout> 

</LinearLayout> 

我已經標註在上面的代碼中的Android Studio顯示佈局錯誤。

使用上述佈局,我得到輸出:

enter image description here

我應當怎​​樣更改有佈局像第一張圖片?

回答

3

好吧,線性佈局有令人驚訝的困難,所以如果你不相對佈局,這可以做類似的事情。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:orientation="vertical" 
     android:layout_weight="0.42"> 

     <ListView 
      android:id="@+id/ist" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight=".8" 
      android:layout_alignParentTop="true" 
      android:layout_above="@+id/linearLayoutHolder" 
      android:background="@color/Bkgd" 
      android:transcriptMode="alwaysScroll" 
      android:stackFromBottom="true" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:id="@+id/linearLayoutHolder" 
      android:layout_alignParentBottom="true" 
      android:layout_weight=".2" 
      android:background="@android:color/white" > 

      <Button android:id="@+id/Btn" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/input_btn" 
        android:background="@android:color/white" /> 

     </LinearLayout> 

    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.58" 
     android:background="@color/colorPrimary" > 

     <GridView 
      android:id="@+id/gridview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="5dp" 
      android:columnWidth="30dp" 
      android:numColumns="auto_fit" 
      android:horizontalSpacing="10dp" 
      android:stretchMode="columnWidth" 
      android:gravity="center" /> 

    </LinearLayout> 

</LinearLayout> 
+0

只是讓ListView的高度WRAP_CONTENT(因爲現在0 DP無所謂),並從ListView和linearlayoutholder刪除佈局權重,並刪除方向(不需要)從相對佈局 – user5155835

+0

然後我就標誌着你的答案 – user5155835

0

其實你忘了在佈局中定義方向爲0.42。

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.42" 
     android:orientation="vertical"> 

但我不知道是否會與舊的API工作,所以使用RelativeLayouts給人更穩定的結果。

0

在全球範圍內,你應該避免使用嵌套重量(由谷歌建議,並表明,像在Android Studio中的警告)

你可以只用一個像這樣的疊瓦(放置的LinearLayout前的按鈕)創建您的觀點:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:orientation="vertical" 
     android:layout_weight="0.42"> 

     <Button 
      android:id="@+id/Btn" 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_alignParentBottom="true" 
      android:src="@drawable/input_btn" 
      android:background="@android:color/white" /> 

     <ListView 
      android:id="@+id/ist" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentTop="true" 
      android:layout_above="@+id/Btn" 
      android:background="@color/Bkgd" 
      android:transcriptMode="alwaysScroll" 
      android:stackFromBottom="true" /> 

    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.58" 
     android:background="@color/colorPrimary" > 

     <GridView 
      android:id="@+id/gridview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="5dp" 
      android:columnWidth="30dp" 
      android:numColumns="auto_fit" 
      android:horizontalSpacing="10dp" 
      android:stretchMode="columnWidth" 
      android:gravity="center" /> 

    </LinearLayout> 

</LinearLayout>