Skip to content Skip to sidebar Skip to footer

Android Fragment Layout Issue (centre Buttons Vertically And Horizontally)

I currently have this layout file which is used for a fragment inside tabs. Copy

Solution 2:

You almost had it, try and change your LinearLayout like this.-

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

You also can remove the FrameLayout, your xml would look like this.-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical" >

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="3dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="10dip" >

        <Button
            android:id="@+id/Btn_Show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="3dip"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:paddingTop="10dip" >

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 4" />
    </TableRow>

</LinearLayout>

As a side note, you should consider keeping your dimensions values in a dimens resource file (i.e. dimens.xml)


Post a Comment for "Android Fragment Layout Issue (centre Buttons Vertically And Horizontally)"