【问题标题】:Android GridLayout placement issueAndroid GridLayout 放置问题
【发布时间】:2013-12-01 02:25:43
【问题描述】:

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

我正在尝试通过定义以下布局 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 列,并且只占底行)以及如何更改此布局以正确显示,如图所示?

【问题讨论】:

  • 可能是因为linearlayout和relativelayout都是空的?尝试在两者中都包含一个元素。
  • 我在空白布局中添加了一些内容。但是它并没有解决问题:S

标签: android android-layout android-gridlayout


【解决方案1】:

我没有设法使用 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2011-08-08
    相关资源
    最近更新 更多