簡體   English   中英

如何在運行時獲取設備高度和寬度?

[英]How to Get Device Height and Width at Runtime?

我正在開發一個應用程序,我必須使我們的應用程序適合每個設備 - 平板電腦和Android手機。 現在我想在運行時獲取設備的高度和寬度,並在我的代碼中實現它,以便我的應用程序適用於橫向和縱向。

我的XML文件是:

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

我的Java文件是:

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();

這樣你就可以獲得屏幕尺寸。

由於此API在新SDK版本中進行了描述,因此您可以使用此API。

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

在Activity范圍中執行:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int w = dm.widthPixels; // etc...

在你的活動的onCreate你可以做

mScreenWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth();

mScreenHeight = getActivity().getWindowManager().getDefaultDisplay().getHeight();

然后使用這些變量來訪問設備的高度和寬度

這就是獲得可用屏幕尺寸的方法。 這將使您不是原始像素大小,而是窗口/活動的可用空間。

    Point outSize = new Point();
    getWindowManager().getDefaultDisplay().getSize(outSize);

您還可以為橫向和縱向使用不同的布局xml文件。 將您的xml用於res / layout-port中的肖像。 景觀布局可以放入res / layout-land。 你應該讀懂android如何處理資源

您可以使用“顯示度量標准”類http://developer.android.com/reference/android/util/DisplayMetrics.html獲取所有與顯示相關的信息。

你需要

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

在此之后,您的指標對象中將顯示所有必需的信息。

另一種選擇是打電話

getActivity().getWindowManager().getDefaultDisplay().getWidth()
getActivity().getWindowManager().getDefaultDisplay().getHeight()

我不打算告訴你如何獲得屏幕尺寸,因為其他人已經這樣做了。 我指的是android開發者開發指南的一個鏈接 ,它應該教你如何設計和開發不同屏幕尺寸的設備。

閱讀后,請再次告訴我們您仍然想要獲得屏幕的寬度和高度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM