刚开始学android,好多不会,照着书做例子,一个数独游戏的开始界面,前几天不知道怎么搞,最后七七八八的能运行了,可是现在这个界面不居中,想问下怎么居中。
这个是运行出来的图
这个是res/layout/activity.xml文件
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:background="@color/background"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_gravity="center">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="25sp"
                android:textColor="#7ba6df"
                android:text="@string/activity_main_title"/>
            <Button
                android:id="@+id/continue_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/continue_lable"/>
            <Button
                android:id="@+id/new_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/new_game_lable"/>
            <Button
                android:id="@+id/about_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/about_lable"/>
            <Button
                android:id="@+id/exit_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/exit_lable"/>
     </LinearLayout>
    </LinearLayout>
这个是res/values/string.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="activity_main_title">android sudoku</string>
    <string name="continue_lable">开始游戏</string>
    <string name="about_lable">游戏说明</string>
    <string name="new_game_lable">是否开启音乐</string>
    <string name="exit_lable">退出游戏</string></resources>
这个是src下的java文件Mainactivity.java
package com.example.helloandroid;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}

解决方案 »

  1.   

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout
            android:background="@color/background"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:gravity="center_horizontal"
            xmlns:android="http://schemas.android.com/apk/res/android">
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_gravity="center">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textSize="25sp"
                    android:textColor="#7ba6df"
                    android:text="@string/activity_main_title"/>
                <Button
                    android:id="@+id/continue_button"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/continue_lable"/>
                <Button
                    android:id="@+id/new_button"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/new_game_lable"/>
                <Button
                    android:id="@+id/about_button"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/about_lable"/>
                <Button
                    android:id="@+id/exit_button"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/exit_lable"/>
            </LinearLayout>
        </LinearLayout>
      

  2.   

     android:gravity="center_horizontal"不过,你这模拟器。。
      

  3.   

    其实有很多种方式可以居中。我给你提供三种:1.android:gravity="center_vertical|center_horizontal"
    2.android:centerInParent="true"
    3.android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"