Android GridLayout放置问题

时间:2013-11-29 23:22:03

标签: android android-layout android-gridlayout

我目前正在使用GridLayout来获取如下所示的视图: enter image description here

我尝试通过定义以下布局xml来创建此布局:

<android.support.v7.widget.GridLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="5"
android:rowCount="2" >

   <LinearLayout 
      android:id="@+id/banner"
      android:layout_row="0"
      android:layout_columnSpan="5"
      android:orientation="horizontal" />


   <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_row="1"
        android:layout_columnSpan="2" />

   <RelativeLayout android:id="@+id/previewpane"
    android:layout_row="1"
    android:layout_column="2"
        android:layout_columnSpan="3"/>

</android.support.v7.widget.GridLayout>

虽然这可以编译并运行而不会崩溃,但ViewPager因某种原因占用了所有屏幕空间。为什么会发生这种情况(看到它只占据5列中的2列而只占据最下面一行),如何更改此布局以正确显示如图所示?

1 个答案:

答案 0 :(得分:0)

我没有设法使用GridLayout正确使用,所以最后我决定只使用RelativeLayout / LinearLayout构造。 (使用RelativeLayout作为根视图以防止必须使用嵌套权重)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/default_bg_color">
     <LinearLayout android:id="@+id/banner"
                   android:orientation="horizontal"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:visibility="gone"
                   android:layout_centerHorizontal="true"
                   android:layout_alignParentTop="true"/>
     <LinearLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:layout_alignParentBottom="true"
         android:layout_below="@id/banner"
         >

         <android.support.v4.view.ViewPager
             android:id="@+id/viewPager"
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="1" />

         <RelativeLayout android:id="@+id/previewPane"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"/>
    </LinearLayout>
</RelativeLayout>
相关问题