<bdo id='xY6AO'></bdo><ul id='xY6AO'></ul>
    1. <small id='xY6AO'></small><noframes id='xY6AO'>

        <tfoot id='xY6AO'></tfoot>
        <i id='xY6AO'><tr id='xY6AO'><dt id='xY6AO'><q id='xY6AO'><span id='xY6AO'><b id='xY6AO'><form id='xY6AO'><ins id='xY6AO'></ins><ul id='xY6AO'></ul><sub id='xY6AO'></sub></form><legend id='xY6AO'></legend><bdo id='xY6AO'><pre id='xY6AO'><center id='xY6AO'></center></pre></bdo></b><th id='xY6AO'></th></span></q></dt></tr></i><div id='xY6AO'><tfoot id='xY6AO'></tfoot><dl id='xY6AO'><fieldset id='xY6AO'></fieldset></dl></div>
        <legend id='xY6AO'><style id='xY6AO'><dir id='xY6AO'><q id='xY6AO'></q></dir></style></legend>

        如何在运行时获取设备高度和宽度?

        时间:2023-06-26
      1. <i id='tkLjB'><tr id='tkLjB'><dt id='tkLjB'><q id='tkLjB'><span id='tkLjB'><b id='tkLjB'><form id='tkLjB'><ins id='tkLjB'></ins><ul id='tkLjB'></ul><sub id='tkLjB'></sub></form><legend id='tkLjB'></legend><bdo id='tkLjB'><pre id='tkLjB'><center id='tkLjB'></center></pre></bdo></b><th id='tkLjB'></th></span></q></dt></tr></i><div id='tkLjB'><tfoot id='tkLjB'></tfoot><dl id='tkLjB'><fieldset id='tkLjB'></fieldset></dl></div>
          1. <tfoot id='tkLjB'></tfoot>

            • <legend id='tkLjB'><style id='tkLjB'><dir id='tkLjB'><q id='tkLjB'></q></dir></style></legend>
                <tbody id='tkLjB'></tbody>
                <bdo id='tkLjB'></bdo><ul id='tkLjB'></ul>

                <small id='tkLjB'></small><noframes id='tkLjB'>

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

                  问题描述

                  我正在开发一个应用程序,我必须使我们的应用程序适合各种设备 - 平板电脑和安卓手机.现在我想在运行时获取设备的高度和宽度,并在我的代码中实现它,以便我的应用程序适用于横向和纵向.

                  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.

                  我的 XML 文件是:

                  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>
                  

                  我的 Java 文件是:

                  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","Ozs",
                              "CAUr","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.

                  由于此 API 在新的 SDK 版本中已被弃用,因此您可以使用它.

                  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;
                  

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

                  上一篇:如何以编程方式检查 Android 中互联网连接的可用性? 下一篇:Android 应用程序不从 Eclipse 启动

                  相关文章

                • <legend id='QVZIq'><style id='QVZIq'><dir id='QVZIq'><q id='QVZIq'></q></dir></style></legend>

                    • <bdo id='QVZIq'></bdo><ul id='QVZIq'></ul>

                    1. <small id='QVZIq'></small><noframes id='QVZIq'>

                      <i id='QVZIq'><tr id='QVZIq'><dt id='QVZIq'><q id='QVZIq'><span id='QVZIq'><b id='QVZIq'><form id='QVZIq'><ins id='QVZIq'></ins><ul id='QVZIq'></ul><sub id='QVZIq'></sub></form><legend id='QVZIq'></legend><bdo id='QVZIq'><pre id='QVZIq'><center id='QVZIq'></center></pre></bdo></b><th id='QVZIq'></th></span></q></dt></tr></i><div id='QVZIq'><tfoot id='QVZIq'></tfoot><dl id='QVZIq'><fieldset id='QVZIq'></fieldset></dl></div>
                      <tfoot id='QVZIq'></tfoot>