2015-01-06 57 views
1

我試圖佈局使用GridLayout一種Android屏幕的Android網格佈局橫向填充,這裏是一個模擬的佈局展示什麼,我試圖完成:動態片段

Layout Mockup

我翻譯的是樣機成佈局和這裏的結果在模擬器中運行:

enter image description here

我要在中央的黑色部分使用盡可能多的水平空間possibl e,如在模擬佈局中。但正如你所看到的,右邊的白色組件正在使用所有可用的水平空間。

我得到在日誌中這樣的錯誤:

01-06 08:27:38.045: D/android.widget.GridLayout(7745): horizontal constraints: x2-x0>=71, x1-x0>=64, x2-x1>=128, x3-x2>=1280, x3-x0<=1280 are inconsistent; permanently removing: x3-x0<=1280. 

我使用2個FrameLayout組件的佔位符動態加載片段。例如,取決於在紅色面板中單擊哪個按鈕,將不同的片段加載到黑色面板中。我看到的另一個相關問題是黑色面板會根據加載的片段中有多少個按鈕來改變大小。

如果可能,我寧願使用GridLayout,但也許我想用GridLayout做什麼是不可能的?我嘗試使用線性和相對佈局來處理此佈局,但無法正確使用。

這是我的佈局文件:

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

    <FrameLayout 
     android:id="@+id/category_placeholder" 
     android:layout_column="0" 
     android:layout_gravity="fill_vertical" 
     android:background="#0f0" 
     android:gravity="start" 
    /> 

    <FrameLayout 
     android:id="@+id/menu_item_placeholder" 
     android:layout_column="1" 
     android:layout_gravity="fill_horizontal|fill_vertical" 
     android:background="#00f" 
     android:gravity="start" 
    /> 

    <fragment 
     android:id="@+id/cart" 
     android:layout_column="2" 
     android:layout_gravity="fill_vertical" 
     android:layout_rowSpan="2" 
     class="com.example.splitscreen.OrderFragment" 
     android:background="#4b0082" 
     android:gravity="end" /> 

    <TextView 
     android:id="@+id/xxx" 
     android:layout_column="0" 
     android:layout_columnSpan="2" 
     android:layout_gravity="fill_horizontal" 
     android:layout_row="1" 
     android:background="#0f0" 
     android:gravity="center" 
     android:padding="25dp" 
     android:text="xxx" 
     android:textColor="#0f0" > 
    </TextView> 
</GridLayout> 
+0

如果你不需要白色內容,爲什麼你有'columnCount = 3'? –

回答

0

我不知道這是否是「最佳」的方法或沒有,但我最終通過返回到使用嵌套LinearLayout解決這個。這似乎給了我想要/需要的東西,允許我通過提供權重來按比例調整我的組件。我花了一點時間才弄清楚它,但我終於明白了。這裏是什麼結果如下:

Nested LinearLayout

而這裏的佈局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:animateLayoutChanges="true" 
    android:baselineAligned="false" 
    android:orientation="vertical" 
    android:weightSum="8" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="6" 
     android:baselineAligned="false" 
     android:orientation="horizontal" 
     android:weightSum="8" > 

     <FrameLayout 
      android:id="@+id/category_placeholder" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="@android:color/holo_red_dark" 
      android:gravity="center" 
      android:textColor="#000" > 
     </FrameLayout> 

     <FrameLayout 
      android:id="@+id/menu_item_placeholder" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="5" 
      android:background="#00f" 
      android:gravity="center" 
      android:textColor="#fff" > 
     </FrameLayout> 

     <fragment 
      android:id="@+id/cart" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="2" 
      class="com.example.splitscreen.OrderFragment" 
      android:background="#4b0082" 
      android:gravity="center" 
      android:textColor="#fff" > 
     </fragment> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/xxx" 
     android:background="#0f0" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2" 
     android:padding="25dp" 
     android:text="xxx" 
     android:textColor="#0f0" > 
    </TextView> 
</LinearLayout> 

一個小小的警告似乎是,我得到一個警告「嵌套的權重是壞的表現」,但目前這似乎是我能夠按照自己的方式工作和佈局的最佳/唯一選項。