如何获取设备的高度和宽度,在运行时? [英] How to Get Device Height and Width at Runtime?

查看:97
本文介绍了如何获取设备的高度和宽度,在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序中,我必须让我们的应用程序,以适应每个设备 - 为平板电脑和Android手机。现在,我想在运行时该设备的高度和宽度,并在我的code实现它让我的应用程序适用于风景和肖像。

我的XML文件是:

< XML版本=1.0编码=UTF-8&GT?; <的LinearLayout   的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android   机器人:layout_width =match_parent   机器人:layout_height =match_parent   机器人:layout_weight =2安卓方向=垂直>     <的LinearLayout机器人:ID =@ + ID / linearLayout1机器人:layout_height =match_parent     机器人:方向=横向机器人:layout_width =match_parent     机器人:layout_weight =1>         <的LinearLayout机器人:ID =@ + ID / linearLayout1机器人:layout_height =match_parent         机器人:方向=垂直机器人:layout_width =match_parent          机器人:layout_weight =1机器人:weightSum =100>             < TextView的机器人:ID =@ + ID / letter1机器人:比重=中心的Andr​​oid:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT机器人:layout_weight =60>< / TextView的>             < TextView的机器人:ID =@ + ID / letter2机器人:比重=中心的Andr​​oid:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT机器人:layout_weight =20>< / TextView的>             < TextView的机器人:ID =@ + ID / letter3机器人:比重=中心的Andr​​oid:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT机器人:layout_weight =20>< / TextView的>         < / LinearLayout中>         <的LinearLayout机器人:ID =@ + ID / linearLayout1机器人:layout_height =match_parent         机器人:重力=中心         机器人:方向=横向机器人:layout_width =match_parent         机器人:layout_weight =1>             < ImageView的机器人:ID =@ + ID / IMAG             机器人:重力=中心             机器人:scaleType =fitCenter             机器人:layout_height =FILL_PARENT             机器人:layout_width =FILL_PARENT              机器人:layout_gravity =中心>         < / ImageView的>         < / LinearLayout中>     < / LinearLayout中>         < LinearLayout中的android:layout_gravity =底         机器人:ID =@ + ID / linearLayout2         机器人:layout_height =WRAP_CONTENT机器人:方向=横向机器人:layout_width =match_parent>             <按钮机器人:ID =@ + ID / previous机器人:layout_width =WRAP_CONTENT机器人:layout_weight =1机器人:文本=previous机器人:layout_height =WRAP_CONTENT> < /按钮>             <按钮机器人:ID =@ + ID /实践机器人:layout_width =WRAP_CONTENT机器人:layout_weight =1机器人:文本=实践的Andr​​oid版本:layout_height =WRAP_CONTENT安卓的onClick =的onClick&GT ;< /按钮>             <按钮机器人:ID =@ + ID /家机器人:layout_width =WRAP_CONTENT机器人:layout_weight =1机器人:文本=家的Andr​​oid版本:layout_height =WRAP_CONTENT>< /按钮>             <按钮机器人:ID =@ + ID /法术机器人:layout_width =WRAP_CONTENT机器人:layout_weight =1机器人:文本=拼的Andr​​oid版本:layout_height =WRAP_CONTENT安卓的onClick =内容&GT ;< /按钮>             <按钮机器人:ID =@ + ID /下一个机器人:layout_width =WRAP_CONTENT机器人:layout_weight =1机器人:文本=下一步机器人:layout_height =WRAP_CONTENT安卓的onClick =内容&GT ;< /按钮>         < / LinearLayout中> < / LinearLayout中>

我的Java文件是:

包com.android; 进口android.app.Activity; 进口android.content.Intent; 进口android.graphics.Bitmap; 进口android.graphics.BitmapFactory; 进口android.graphics.Typeface; 进口android.media.MediaPlayer; 进口android.os.Bundle; 进口android.view.MotionEvent; 进口android.view.View; 进口android.view.View.OnClickListener; 进口android.widget.Button; 进口android.widget.ImageView; 进口android.widget.TextView; 进口android.widget.Toast; 进口com.android.SimpleGestureFilter.SimpleGestureListener; 公共类MySwara扩展活动实现SimpleGestureListener {     私人SimpleGestureFilter检测器;     私有静态诠释柜台= -1;     私有String [] mBtn1 = {的C,D,E,F,G,H,IA,J,K,L,M ,N,O,CA,CB};     私人TextView的txtLetter;     私人ImageView的imgLetter;     私人诠释[] imgArr={R.drawable.w1,R.drawable.w2,R.drawable.w3,R.drawable.w4,R.drawable.w5,R.drawable.w6,R.drawable.w7,R.drawable.w8,R.drawable.w9,R.drawable.w10,R.drawable.w11,R.drawable.w12,                             R.drawable.w13,R.drawable.w14,R.drawable.w15};     私人TextView的txtKannada;     私有String [] mBtn2 = {"CgÀ¸À","DªÉÄ","E°","F±À","GqÀ","Hl","IĶ","J¯É","Kr","LzÀÄ","M¯É","N¯É","OµÀzsÀ",             CAUÀr,CB};     私有String [] mBtn3 = {ARASA,AME,流感样病例,ISA,UDA,UTA,RUSHI,ELE,电子数据交换,爱都,奥莱报 奥莱报,AUSHADA             ANGADI,AHA};     私人TextView的txtEnglish;     私人INT [] mAudio = {R.raw.a,R.raw.b,R.raw.c,R.raw.d,R.raw.e,R.raw.f,R.raw.g,R .raw.h,R.raw.i,R.raw.j,             R.raw.k,R.raw.l,R.raw.m,R.raw.n,R.raw.o};     保护MediaPlayer的熔点;     @覆盖     保护无效的onCreate(包savedInstanceState){         // TODO自动生成方法存根             super.onCreate(savedInstanceState);             的setContentView(R.layout.content);             探测器=新SimpleGestureFilter(这一点,这一点);             如果(计数器== -1)                 。计数器= getIntent()getExtras()调用getInt(位置);             字样TF = Typeface.createFromAsset(getBaseContext()getAssets(),字体/ brhknd.ttf。);             txtLetter =(TextView中)findViewById(R.id.letter1);             txtKannada =(TextView中)findViewById(R.id.letter2);             txtEnglish =(TextView中)findViewById(R.id.letter3);             imgLetter =(ImageView的)findViewById(R.id.imag);             txtLetter.setTypeface(TF);             txtLetter.setText(mBtn1 [窗口]);             //txtLetter.setTextSize(350);             位图BM = BitmapFactory.de codeResource(getResources(),imgArr [计数器]);             imgLetter.setImageBitmap(BM);             txtKannada.setTypeface(TF);             txtKannada.setText(mBtn2 [窗口]);             //txtKannada.setTextSize(100);             txtEnglish.setText(mBtn3 [窗口]);             //txtEnglish.setTextSize(50);             按钮btnNext =(按钮)findViewById(R.id.next);             btnNext.setOnClickListener(新OnClickListener(){                 @覆盖                 公共无效的onClick(视图v){                     如果(柜< imgArr.length-1)                         反++;                     changeContent();                 }             });             按钮mPlay =(按钮)findViewById(R.id.spell);             mPlay.setOnClickListener(新OnClickListener(){                 @覆盖                 公共无效的onClick(视图v){                     MP = MediaPlayer.create(MySwara.this,mAudio [计数器]);                     mp.start();                 }             });             按钮btnPrvs =(按钮)findViewById(R.id. previous);             btnP​​rvs.setOnClickListener(新OnClickListener(){                 @覆盖                 公共无效的onClick(视图v){                     如果(计数器大于0)                         计数器 - ;                     changeContent();                 }             });             按钮btnPractice =(按钮)findViewById(R.id.practice);             btnP​​ractice.setOnClickListener(新OnClickListener(){                 @覆盖                 公共无效的onClick(视图v){                     意向意图=新的意图(MySwara.this,DrawingActivity.class);                     startActivity(意向);                 }             });             按钮btnHome =(按钮)findViewById(R.id.home);             btnHome.setOnClickListener(新OnClickListener(){                 @覆盖                 公共无效的onClick(视图v){                     意向意图=新的意图(MySwara.this,mainClass.class);                     startActivity(意向);                 }             });     }     公共无效changeContent()     {         txtLetter.setText(mBtn1 [窗口]);         txtKannada.setText(mBtn2 [窗口]);         txtEnglish.setText(mBtn3 [窗口]);         //imgLetter.setBackgroundResource(imgArr[counter]);         位图BM = BitmapFactory.de codeResource(getResources(),imgArr [计数器]);         imgLetter.setImageBitmap(BM);     } @覆盖         公共布尔dispatchTouchEvent(MotionEvent我){           this.detector.onTouchEvent(箱);          返回super.dispatchTouchEvent(箱);         }     @覆盖      公共无效onSwipe(INT方向){       字符串str =;       开关(方向){       案例SimpleGestureFilter.SWIPE_RIGHT:海峡=向右滑动;                                                 如果(计数器大于0)                                                     计数器 - ;                                                     changeContent();                                                打破;       案例SimpleGestureFilter.SWIPE_LEFT:海峡=刷卡左;                                             如果(柜< imgArr.length-1)                                                 反++;                                                 changeContent();                                                      打破;       }      }     @覆盖     公共无效onDoubleTap(){         // TODO自动生成方法存根     }     }

解决方案

 显示mDisplay = activity.getWindowManager()getDefaultDisplay()。
最终诠释宽度= mDisplay.getWidth();
最终诠释身高= mDisplay.getHeight();
 

这样,您就可以得到屏幕的大小。

由于这个API是depricated在新的SDK版本,你可以用这个。

  DisplayMetrics displayMetrics = context.getResources()getDisplayMetrics()。
INT宽度= displayMetrics.widthPixels;
INT高= displayMetrics.heightPixels;
 

I am developing an app in which I have to make our app to fit for every device - for both tablet and android mobiles. Now I want to get the device height and width at runtime and implement it in my code so that my app works for both landscape and portrait.

My XML file is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="2" android:orientation="vertical">

    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="match_parent" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_weight="1">

        <LinearLayout android:id="@+id/linearLayout1" android:layout_height="match_parent"  
        android:orientation="vertical" android:layout_width="match_parent" 
         android:layout_weight="1" android:weightSum="100">
            <TextView  android:id="@+id/letter1" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content"  android:layout_weight="60"></TextView>
            <TextView  android:id="@+id/letter2" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content"  android:layout_weight="20"></TextView>
            <TextView  android:id="@+id/letter3" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content"  android:layout_weight="20"></TextView>
        </LinearLayout>

        <LinearLayout android:id="@+id/linearLayout1" android:layout_height="match_parent"  
        android:gravity="center"
        android:orientation="horizontal" android:layout_width="match_parent" 
        android:layout_weight="1">
            <ImageView android:id="@+id/imag"  
            android:gravity="center"
            android:scaleType = "fitCenter"
            android:layout_height="fill_parent" 
            android:layout_width="fill_parent"
             android:layout_gravity="center">
        </ImageView>

        </LinearLayout>

    </LinearLayout>


        <LinearLayout android:layout_gravity="bottom" 
        android:id="@+id/linearLayout2" 
        android:layout_height="wrap_content" android:orientation="horizontal" android:layout_width="match_parent">
            <Button android:id="@+id/previous" android:layout_width="wrap_content" android:layout_weight="1" android:text="Previous" android:layout_height="wrap_content" ></Button>
            <Button android:id="@+id/practice" android:layout_width="wrap_content" android:layout_weight="1" android:text="Practice" android:layout_height="wrap_content" android:onClick="onClick"></Button>
            <Button android:id="@+id/home" android:layout_width="wrap_content" android:layout_weight="1" android:text="Home" android:layout_height="wrap_content"></Button>
            <Button android:id="@+id/spell" android:layout_width="wrap_content" android:layout_weight="1" android:text="Spell" android:layout_height="wrap_content" android:onClick="Content"></Button>
            <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_weight="1" android:text="Next" android:layout_height="wrap_content" android:onClick="Content"></Button>


        </LinearLayout>

</LinearLayout>

My Java file is:

package com.android;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.SimpleGestureFilter.SimpleGestureListener;

public class MySwara extends Activity implements SimpleGestureListener {


    private SimpleGestureFilter detector;
    private static int counter=-1;
    private String[] mBtn1 ={"C","D","E","F","G","H","IÄ","J","K","L","M","N","O","CA","CB"};
    private TextView txtLetter;
    private ImageView imgLetter;

    private int[] imgArr={R.drawable.w1,R.drawable.w2,R.drawable.w3,R.drawable.w4,R.drawable.w5,R.drawable.w6,R.drawable.w7,R.drawable.w8,R.drawable.w9,R.drawable.w10,R.drawable.w11,R.drawable.w12,
                            R.drawable.w13,R.drawable.w14,R.drawable.w15};
    private TextView txtKannada;
    private String[] mBtn2 = {"CgÀ¸À","DªÉÄ","E°","F±À","GqÀ","Hl","IĶ","J¯É","Kr","LzÀÄ","M¯É","N¯É","OµÀzsÀ",
            "CAUÀr","CB"};
    private String[] mBtn3 = {"ARASA","AME","ILI","ISA","UDA","UTA","RUSHI","ELE","EDI","AIDU","oLE","OLE","AUSHADA",
            "ANGADI","AHA"};
    private TextView txtEnglish;

    private int[] mAudio = {R.raw.a,R.raw.b,R.raw.c,R.raw.d,R.raw.e,R.raw.f,R.raw.g,R.raw.h,R.raw.i,R.raw.j,
            R.raw.k,R.raw.l,R.raw.m,R.raw.n,R.raw.o};
    protected MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.content);   

            detector = new SimpleGestureFilter(this,this);
            if(counter == -1)
                counter =getIntent().getExtras().getInt("POSITION");
            Typeface tf = Typeface.createFromAsset(getBaseContext().getAssets(), "fonts/brhknd.ttf");
            txtLetter = (TextView)findViewById(R.id.letter1);
            txtKannada = (TextView)findViewById(R.id.letter2);
            txtEnglish = (TextView)findViewById(R.id.letter3);
            imgLetter = (ImageView)findViewById(R.id.imag);
            txtLetter.setTypeface(tf);      
            txtLetter.setText(mBtn1[counter]);
            //txtLetter.setTextSize(350);

            Bitmap bm = BitmapFactory.decodeResource(getResources(), imgArr[counter]);
            imgLetter.setImageBitmap(bm);

            txtKannada.setTypeface(tf);
            txtKannada.setText(mBtn2[counter]);
            //txtKannada.setTextSize(100);

            txtEnglish.setText(mBtn3[counter]);
            //txtEnglish.setTextSize(50);



            Button btnNext =  (Button)findViewById(R.id.next);
            btnNext.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {


                    if(counter<imgArr.length-1)
                        counter++;
                    changeContent();
                }
            });

            Button mPlay = (Button)findViewById(R.id.spell);
            mPlay.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    mp = MediaPlayer.create(MySwara.this, mAudio[counter]);   
                    mp.start();

                }
            });


            Button btnPrvs =  (Button)findViewById(R.id.previous);
            btnPrvs.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {


                    if(counter>0)
                        counter--;
                    changeContent();
                }
            });


            Button btnPractice = (Button)findViewById(R.id.practice);
            btnPractice.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MySwara.this,DrawingActivity.class);
                    startActivity(intent);

                }
            });
            Button btnHome = (Button)findViewById(R.id.home);
            btnHome.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MySwara.this,mainClass.class);
                    startActivity(intent);

                }
            });
    }



    public void changeContent()
    {

        txtLetter.setText(mBtn1[counter]);
        txtKannada.setText(mBtn2[counter]);
        txtEnglish.setText(mBtn3[counter]);
        //imgLetter.setBackgroundResource(imgArr[counter]);
        Bitmap bm = BitmapFactory.decodeResource(getResources(), imgArr[counter]);
        imgLetter.setImageBitmap(bm);

    }


@Override
        public boolean dispatchTouchEvent(MotionEvent me){
          this.detector.onTouchEvent(me);
         return super.dispatchTouchEvent(me);
        }
    @Override
     public void onSwipe(int direction) {
      String str = "";

      switch (direction) {

      case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right";

                                                if(counter>0)
                                                    counter--;
                                                    changeContent();
                                               break;
      case SimpleGestureFilter.SWIPE_LEFT :  str = "Swipe Left";
                                            if(counter<imgArr.length-1)
                                                counter++;
                                                changeContent();
                                                     break;

      }

     }



    @Override
    public void onDoubleTap() {
        // TODO Auto-generated method stub

    }


    }

解决方案

Display mDisplay = activity.getWindowManager().getDefaultDisplay();
final int width  = mDisplay.getWidth();
final int height = mDisplay.getHeight();

This way you can get the screen size.

Since this API is depricated in the new SDK versions you can use this.

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;

这篇关于如何获取设备的高度和宽度,在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆