Android layout drawing glitch

2019-06-12 11:08发布

问题:

I am writing an android game, I have created a layout for the main menu which has this XML:

<?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:orientation="vertical" >

    <TextView
        android:id="@+id/tvStats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:gravity="fill_horizontal" >

        <Button
            android:id="@+id/btnStartSurvive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill_horizontal"
            android:gravity="center_vertical|center_horizontal"
            android:text="Start Survival Mode" />

        <Button
            android:id="@+id/btnStartChall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill_horizontal"
            android:text="Start Callenge Mode" />

    </LinearLayout>

</LinearLayout>

The layout is shown on the screen using this activity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Get rid of the stuff around the edges
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //show layout
        setContentView(R.layout.main_menu);
        Button btnStart = (Button)findViewById(R.id.btnStartSurvive);
        btnStart.setOnClickListener(oclStart);
    }

    private OnClickListener oclStart = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this, GameActivity.class);
            intent.putExtra("gametype", 'S');
            startActivity(intent);  
        }
    };

}

When I run this on my phone (Android 4.2.2) I get something that looks like this:

Clicking the unused button makes it look like this:

When I run this on my older tablet (Android 2.2) I get what I expected:

Is this an Android Bug? How do I get around it?

Or is it my fault? How do I fix it?

回答1:

I've managed to duplicate the same results, on a Galaxy s4, running 4.2.2. Buttons don't seem to work all that well for some reason.

Have you tried changing the theme? Sometimes the theme will mess up a layout.