Android Studios:单击按钮时解锁屏幕方向

时间:2018-08-27 22:49:39

标签: java android

我设置了一个程序,其中有一个图像和一个选择按钮。当您按下选择按钮时,该按钮消失并且出现一个新的后退按钮。我希望应用程序锁定为纵向模式,但是当您按下选择时,我希望屏幕旋转解锁,而侧面转动时,我希望图像填充为屏幕的中心,而后退按钮消失。当再次侧向旋转时,我希望后退按钮重新出现。单击后退按钮时,它消失,选择按钮再次返回。我希望它再次将屏幕锁定为纵向。

如果可能的话,我将非常感谢您的帮助!谢谢!

public class MainActivity extends AppCompatActivity {

private Button select;
private Button right;
private Button left;
private Button back;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

            select = findViewById(R.id.select);
    left = findViewById(R.id.button2);
    right = findViewById(R.id.button3);
    back = findViewById(R.id.back);

    back.setVisibility(View.INVISIBLE);

    select.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            right.setVisibility(View.INVISIBLE);
            left.setVisibility(View.INVISIBLE);
            back.setVisibility(View.VISIBLE);
            select.setVisibility(View.INVISIBLE);
        }
    });

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            right.setVisibility(View.VISIBLE);
            left.setVisibility(View.VISIBLE);
            back.setVisibility(View.INVISIBLE);
            select.setVisibility(View.VISIBLE);
        }
    });
}
}

1 个答案:

答案 0 :(得分:2)

可以通过调用此类方法随时锁定和解锁方向

/** Locks the device window in landscape mode. */
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


/** Locks the device window in portrait mode. */
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


/** Allows user to freely use portrait or landscape mode. */
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

要给他们打电话取决于你自己