Android中两种设置全屏的方法 && Android横竖屏切换

 在开发中经常需要把我们的应用设置为全屏,这里我所知道的有俩中方法,一中是在代码中设置,另一种方法是在配置文件里改!

一、在代码中设置:

import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class Demo extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //无title requestWindowFeature(Window.FEATURE_NO_TITLE); //全屏 getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN); setContentView(R.layout.main); } }

在这里要强调一点,设置全屏的俩段代码必须在setContentView(R.layout.main) 之前,不然会报错。

二、在配置文件里修改(android:theme="@android:style/Theme.NoTitleBar.Fullscreen"):

在这里我还想说明一下,用前者在我们应用运行后,会看到短暂的状态栏,然后才全屏,而第二种方法是不会有这种情况的,所以我建议

大家使用后者.

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------

android横竖屏切换:

public class screenOrientation extends Activity
{
  private TextView mTextView01;
  private Button mButton01;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mButton01 = (Button) findViewById(R.id.myButton1);
    mTextView01 = (TextView) findViewById(R.id.myTextView1);

    /**
     * 需要注意的是要在AndroidManifest.xml/n添加android:screenOrientation属性
     */
    if (getRequestedOrientation() == -1)
    {
      mTextView01.setText(getResources().getText(0,
          "请添加android:screenOrientation属性  "));
    }

    /* 当点击按钮旋转屏幕画面 */
    mButton01.setOnClickListener(new Button.OnClickListener()
    {
      @Override
      public void onClick(View arg0)
      {
        /* 方法一:重写getRequestedOrientation */

        /* 若无法取得screenOrientation属性 */
        if (getRequestedOrientation() == -1)
        {
          /* 提示无法进行画面旋转功能,因无法判别Orientation */
          mTextView01.setText(getResources().getText(R.string.str_err_1001));
        } else
        {
          if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
          {
            /* 若当下为横排,则更改为竖排呈现 */
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
          } else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
          {
            /* 若当下为竖排,则更改为横排呈现 */
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
          }
        }
      }
    });
  }

  /**
   * 若要通过程序改变屏幕的方向,必须要覆盖setRequestedOrientation()方法,
   * 而若要取得当下的屏幕方向,必须要访问getRequestedOrientation()方法。
   * 在AndroidManifest.xml中需要设置android
   * :screenOrientation属性,否则将无法通过getRequestedOrientation()来判断Activity的方向。
   * 
   * @param requestedOrientation
   */
  @Override
  public void setRequestedOrientation(int requestedOrientation)
  {
    // TODO Auto-generated method stub

    /* 判断要更改的方向,以Toast提示 */
    switch (requestedOrientation)
    {
      /* 更改为LANDSCAPE */
      case (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE):
        mMakeTextToast(getResources().getText(R.string.str_msg1).toString(),
            false);
        break;
      /* 更改为PORTRAIT */
      case (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT):
        mMakeTextToast(getResources().getText(R.string.str_msg2).toString(),
            false);
        break;
    }
    super.setRequestedOrientation(requestedOrientation);
  }

  @Override
  public int getRequestedOrientation()
  {
    // TODO Auto-generated method stub

    /* 此重写getRequestedOrientation方法,可取得当下屏幕的方向 */
    return super.getRequestedOrientation();
  }

  public void mMakeTextToast(String str, boolean isLong)
  {
    if (isLong == true)
    {
      Toast.makeText(screenOrientation.this, str, Toast.LENGTH_LONG).show();
    } else
    {
      Toast.makeText(screenOrientation.this, str, Toast.LENGTH_SHORT).show();
    }
  }
}


 

你可能感兴趣的:(android)