Domanda

I created an activity with GridLayout to show 4 buttons on the screen - they displayed in the centre of the screen both horizontally and vertically.

I have now changed the activity to a fragment that shows inside Tabs using a guide found on http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

But the tabs are showing all aligned to the top left hand side of the screen.

**I want a display with the 4 buttons in the middle of the screen both horizontally and vertically *


 BTN 1    BTN 2


 BTN 3    BTN 4

this is my layout file of the fragment

<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>

this is the main layout for the activity which includes the fragment as a tab

<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>
È stato utile?

Soluzione

i think you need to replace GridLayout with TableRow

this layout for example as your request

<?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>

results

enter image description here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top