Android擷取螢幕大小和設定無標題【安卓進化二十一】

來源:互聯網
上載者:User

       android擷取螢幕大小非常常用,例如寫個程式,如果要做成通用性很強的程式,適用螢幕很強,一般布局的時候都是根據螢幕的長寬來定義的,所以我把這個總結一下,方便日後忘記的時候查閱。還有就是有時候寫程式根據需求不需要title,可以在程式中設定無title的螢幕!轉載請標明出處:

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

 

                                                                程式的:

                                                        

代碼說明一切真理:

一、mainActivity。java類得代碼:

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);        //設定為無標題        requestWindowFeature(Window.FEATURE_NO_TITLE);        //設定為全螢幕模式        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,         WindowManager.LayoutParams.FLAG_FULLSCREEN);        setContentView(R.layout.main);        //定義DisplayMetrics對象        DisplayMetrics dm = new DisplayMetrics();        //擷取視窗屬性        getWindowManager().getDefaultDisplay().getMetrics(dm);        //視窗寬度        int screenWidth = dm.widthPixels;        //視窗高度        int screenHeight = dm.heightPixels;                mTextView = (TextView)findViewById(R.id.text_view);        mTextView.setText("螢幕寬度: "+screenWidth+                   "\n螢幕高度:  "+screenHeight);    }}

 

二、main.xml布局檔案的代碼:

<?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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.