2

这是一个玩具示例。如果您在实际设备(而不是模拟器)上编译并测试,您将看到第一次附加 SurfaceView 时,整个屏幕内容会相应更改。但是,在按回然后再次按按钮时,您会看到顶部的一条细线与上一个屏幕保持蓝色,并且仅在创建 Surface 后消失。这在我的所有视图(无 SurfaceView)中都发生在我的应用程序中,并且令人烦恼。有没有人见过这个问题?有解决方法吗?我在 Android 2.1 上的 HTC EVO 上运行它

package com.example.surfaceviewtest;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

    private RelativeLayout home;
    private RelativeLayout newView;
    private int view;

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        home = new RelativeLayout(this);
        home.setBackgroundColor(Color.BLUE);
        Button changeCV = new Button(this);
        changeCV.setText("Change Content View");
        changeCV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(newView, new RelativeLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                view = 2;
            }
        });
        RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        buttonParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        home.addView(changeCV, buttonParams);

        newView = new RelativeLayout(this);
        RelativeLayout.LayoutParams surfaceParams = new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT, 200);
    surfaceParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        SurfaceView surfView = new SurfaceView(this);
        newView.addView(surfView, surfaceParams);
        RelativeLayout.LayoutParams tvParams = new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        tvParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        TextView tv = new TextView(this);
        newView.addView(tv, tvParams);

        setContentView(home, new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        view = 1;
    }

     /* (non-Javadoc)
     * @see android.app.Activity#onBackPressed()
     */
    @Override
    public void onBackPressed() {
        if (view == 2) {
            setContentView(home, new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            view = 1;
        }
        else {
            finish();
        }
    }   
}
4

0 回答 0