继承AppCompatActivity或Activity时,标题

2018-10-10  本文已影响151人  小小程序员jh

Android 开发中容易忽略的点:

 android.util.AndroidRuntimeException: requestFeature() must be called before adding content                            
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2319)                           
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2370)                            
    at android.app.ActivityThread.access$800(ActivityThread.java:155)                           
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)                             
    at android.os.Handler.dispatchMessage(Handler.java:102)         
public class MainActivity extends Activity {     
  @Override 
  protected void onCreate(Bundle savedInstanceState) {               
      super.onCreate(savedInstanceState);               
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      // 这行为隐藏标题栏               
      setContentView(R.layout.activity_main); 
  }
}
// 继承AppCompatActivity时:   
   @Override    
   protected void onCreate(Bundle savedInstanceState) {      
    super.onCreate(savedInstanceState);       
     
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);       
    /** * 去掉系统自带的标题栏*/      
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);      
     // 因为是继承自AppCompatActivity, 所以要加上下面几句代码才能管用       
    if (getSupportActionBar() != null){          
         getSupportActionBar().hide();       
     }       
         setContentView(R.layout.activity_main);   
   }
上一篇下一篇

猜你喜欢

热点阅读