Android gets screen size and sets untitled [Android evolution 21]

Source: Internet
Author: User

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:

Package com.cn. daming; import android. app. activity; import android. OS. bundle; import android. util. displaymetrics; import android. view. window; import android. view. windowmanager; import android. widget. textview; public class mainactivity extends activity {private textview mtextview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // set it to the Untitled requestwindowfeature (window. feature_no_title); // set it to full screen mode getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); setcontentview (R. layout. main); // defines the displaymetrics object displaymetrics dm = new displaymetrics (); // obtains the window attribute getwindowmanager (). getdefadisplay display (). getmetrics (DM); // window width int screenwidth = DM. widthpixels; // The window height int screenheight = DM. heightpixels; mtextview = (textview) findviewbyid (R. id. text_view); mtextview. settext ("screen width:" + screenwidth + "\ n screen height:" + screenheight );}}

 

Ii. Main. xml layout file code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:id="@+id/text_view"    android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_gravity="center_vertical|center_horizontal"    android:gravity="center"    android:text="@string/hello"    android:textSize="18pt"    /></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.