GridLayout在活动和片段中显示不同

时间:2013-11-03 13:40:13

标签: android android-layout android-fragments android-gridlayout

我使用GridLayout创建了一个活动,在屏幕上显示4个按钮 - 它们在水平和垂直方向显示在屏幕中央。

我现在已将活动更改为使用http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/上的指南显示在标签内的片段

但标签显示全部与屏幕左上角对齐。

**我想在屏幕中间水平和垂直显示4个按钮*


 BTN 1    BTN 2


 BTN 3    BTN 4

这是片段的布局文件

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:columnCount="2"
    android:orientation="horizontal" >

 <Button 
            android:id="@+id/Btn_Show"
            android:text="Btn Show"
            android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_column="0"
        android:drawableTop="@drawable/ic_launcher" />
    <Button 
            android:id="@+id/Btn_2"
            android:text="Btn 2"
            android:layout_width="100dp"
        android:layout_height="100dp"
        android:drawableTop="@drawable/ic_launcher" />
    <Button 
            android:id="@+id/Btn_3"
            android:text="Btn 3"
            android:layout_width="100dp"
            android:layout_height="100dp"
        android:drawableTop ="@drawable/ic_launcher" />
    <Button 
            android:id="@+id/Btn_4"
            android:text="Btn 4"
            android:layout_width="100dp"
        android:layout_height="100dp"
        android:drawableTop="@drawable/ic_launcher" />

</GridLayout>

这是活动的主要布局,其中包含片段作为标签

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

1 个答案:

答案 0 :(得分:0)

我认为您需要将GridLayout替换为TableRow

此布局例如作为您的请求

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:orientation="vertical" >

          <TableRow
              android:paddingTop="10dip"
              android:paddingLeft="10dip"
              android:paddingRight="10dip"
              android:paddingBottom="3dip"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
          <Button
            android:id="@+id/button1"
            android:layout_weight=".2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />
          <Button
            android:id="@+id/button2"
            android:layout_weight=".2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2" />
         </TableRow>
          <TableRow
              android:paddingTop="10dip"
              android:paddingLeft="10dip"
              android:paddingRight="10dip"
              android:paddingBottom="3dip"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
          <Button
            android:id="@+id/button3"
            android:layout_weight=".2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3" />
          <Button
            android:id="@+id/button4"
            android:layout_weight=".2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 4" />
        </TableRow>

</LinearLayout>
</FrameLayout>

结果

enter image description here