Android gets the screen size and sets a title bar without a title bar. android title bar

Source: Internet
Author: User

Android gets the screen size and sets a title bar without a title bar. android title bar

Android is very often used to obtain the screen size. For example, if you want to write a program and make a program with strong versatility, the screen is very applicable. Generally, the layout is defined based on the screen length and width, so I will summarize this to facilitate future reference when I forget it. In addition, sometimes writing a program requires no title. You can set a screen without title in the program! Reprinted please indicate the source:

Http://blog.csdn.net/wdaming1986/article/details/6769821

 

Program:

The Code illustrates all truths:

1. mainActivity. Java code:

Java code
  1. Package com.cn. daming;
  2. Import android. app. Activity;
  3. Import android. OS. Bundle;
  4. Import android. util. DisplayMetrics;
  5. Import android. view. Window;
  6. Import android. view. WindowManager;
  7. Import android. widget. TextView;
  8. Public class MainActivity extends Activity {
  9. Private TextView mTextView;
  10. @ Override
  11. Public void onCreate (Bundle savedInstanceState ){
  12. Super. onCreate (savedInstanceState );
  13. // Set to untitled
  14. RequestWindowFeature (Window. FEATURE_NO_TITLE );
  15. // Set to full screen mode
  16. GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
  17. WindowManager. LayoutParams. FLAG_FULLSCREEN );
  18. SetContentView (R. layout. main );
  19. // Define the DisplayMetrics object
  20. DisplayMetrics dm = new DisplayMetrics ();
  21. // Obtain window properties
  22. GetWindowManager (). getDefaultDisplay (). getMetrics (dm );
  23. // Window width
  24. Int screenWidth = dm. widthPixels;
  25. // Window height
  26. Int screenHeight = dm. heightPixels;
  27. MTextView = (TextView) findViewById (R. id. text_view );
  28. MTextView. setText ("screen width:" + screenWidth +
  29. "\ N screen height:" + screenHeight );
  30. }
  31. }

 

Ii. main. xml layout file code:

Html code
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <TextView
  8. Android: id = "@ + id/text_view"
  9. Android: layout_width = "fill_parent"
  10. Android: layout_height = "fill_parent"
  11. Android: layout_gravity = "center_vertical | center_horizontal"
  12. Android: gravity = "center"
  13. Android: text = "@ string/hello"
  14. Android: textSize = "18pt"
  15. />
  16. </LinearLayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.