开发者

XML layout android not aligned perfectly in the center

开发者 https://www.devze.com 2023-03-29 15:06 出处:网络
Here is my xml layout code <?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"

Here is my xml layout code

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/titleset"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Orb Runner"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_marginTop="20dp"
    android:textSize="45sp"
    android:textColor="#FF3366CC"/>

    <TableLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp"
    android:gravity="center"
    android:layout_gravity="center">

    <TableRow android:layout_gravity="center"
    android:gravity="center" android:layout_width="fill_parent">

    <TextView android:id="@+id/newgamebut"
    android:layout_marginRight="5dp"
    android:text="New Game"
    style="@style/Button" android:layout_height="fill_parent" android:layout_width="wrap_content"/>

    <TextView android:id="@+id/newgamebut"
    android:layout_marginLeft="5dp"
    android:text="High Score"
    android:layout_gravity="center"
    style="@style/Button" android:layout_height="fill_parent" android:layout_width="fill_parent"/>

    </TableRow>

    <TableRow android:layout_gravity="center"
    android:gravity="center" android:layout_width="fill_parent">

    <TextView android:id="@+id/newgamebut"
    android:layout_marginRight="5dp"
    android:text="Settings"
    style="@style/Button" android:layout_height="fill_parent" android:layout_width="wrap_content"/>

    <TextView android:id="@+id/newgamebut"
    android:layout_marginLeft="5dp"
    android:text="Instructions"
    android:layout_gravity="center"
    style="@style/Button" android:layout_height="fill_parent" android:layout_width="fill_parent"/>

    </TableRow>

    <TableRow android:layout_gravity="center"
    android:gravity="center" android:layout_width="fill_parent">

    <TextView android:id="@+id/newgamebut"
    android:layout_marginRight="5dp"
    android:text="About"
    style="@style/Button" android:layout_height="fill_parent" android:layout_width="wrap_content"/>

    <TextView android:id="@+id/newgamebut"
    android:layout_marginLeft="5dp"
    android:text="Exit Game"
    android:layout_gravity="center"
    style="@styl开发者_StackOverflow中文版e/Button" android:layout_height="fill_parent" android:layout_width="fill_parent"/>

    </TableRow>

    </TableLayout>
</LinearLayout>

What i want to do is to get the instructions textview/button to be completely aligned with the row on top of it currently this is how it looks like.

| New Game |   | High Scores |
| Settings |  | Instructions |
|   About  |   |  Exit Game  |

I want it to look like this

| New Game |   | High Scores |
| Settings |   | Instructions|
|  About   |   |  Exit Game  |


You can use the android:weightSum and android:layout_weight attributes for the textview/button to assign a relative width.

It might sound weird but I would also recommend using android:layout_width="0dp" for your buttons and textviews when using layout_weight.

0

精彩评论

暂无评论...
验证码 换一张
取 消