0

我想我的问题很简单,所以谢谢你的帮助。我做了一个简单的游戏,终于运行了,但我不能让开始内容显示在页面的中心,谁能帮我解决这个问题?

这是当前页面:

请进入此处查看渲染 代码:

资源/布局/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">开始游戏&lt;/string>
    <string name="about_lable">游戏说明</string>
    <string name="new_game_lable">是否开启音乐&lt;/string>
    <string name="exit_lable">退出游戏&lt;/string>

</resources>

Mainactivity.java

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

}
4

3 回答 3

0

试试这个修改后的 xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:background="@color/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center_horizontal|center_vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal">
            <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>
于 2012-12-05T08:55:47.947 回答
0

您可以添加到布局代码的三个选项

1.

android:gravity="center_vertical|center_horizontal"

2.

android:centerInParent="true"

3.

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
于 2012-12-05T08:58:06.277 回答
0

这将为您工作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:gravity="center" >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/activity_main_title"
            android:textColor="#7ba6df"
            android:textSize="25sp" />

        <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>
于 2012-12-05T08:59:22.220 回答