Android获取手机屏幕大小 — DisplayMetrics取得画面宽高的方法

 Android获取手机屏幕大小 — DisplayMetrics取得画面宽高的方法

编号A3GS_TA20100113003

 

描述

 

本文讲述了DisplayMetrics取得手机屏幕大小的方法。

 

例子

 

有兴趣的朋友可以在登录后下载本文例子代码!

 

例子效果图

 

 

 

实现步骤

 

第一步:建Android 工程:DisplayMetricsDemo

 

第二步:编写Activity 的子类别:DisplayMetricsDemo,其程序代码如下:

 

package com.a3gs.displaymetrics;

 

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.util.DisplayMetrics;

import android.widget.TextView;

 

public class DisplayMetricsDemo extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        /* 必须引用 android.util.DisplayMetrics */

        DisplayMetrics dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);

        String textStr = "手机屏幕的分辨率为:" +

dm.widthPixels + "*" + dm.heightPixels;

        TextView tv = (TextView) findViewById(R.id.myTV);

        tv.setText(textStr);

        tv.setTextColor(Color.RED);

    }

}

http://www.a3gs.com/BookViews.asp?InfoID=2972&classID=942&InfoType=0

 

动态重新设定布局setLayoutParams

 

LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mAutoComplete.getLayoutParams();//获取控件的当前布局

linearParams.height=100;//对该控件的布局参数做修改

mAutoComplete.setLayoutParams(linearParams);//重新设定布局

 

动画

播放动画:

动画用于指定的控件,所有继承自View的控件都有startAnimation(Animation)方法,通过调用此方法来应用动画于控件

加载动画:

为应用动画提供了通用的的方法,它有一个很重要的方法loadAnimation(Context,Animation)用于加载Animation的实例。

 

LayoutInflater

这一节讲的是LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),

不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。

为了让大家容易理解我做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。

  @Override  
    public void onClick(View v) {  
          
        showCustomDialog();  
    }  
      
    public void showCustomDialog()  
    {  
        AlertDialog.Builder builder;  
        AlertDialog alertDialog;  
        Context mContext = LayoutInflaterDemo.this;  
          
        //下面俩种方法都可以  
        LayoutInflater inflater = getLayoutInflater();  
        LayoutInflater inflater = (LayoutInflater)   
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);  
        View layout = inflater.inflate(R.layout.custom_dialog,null);  
        TextView text = (TextView) layout.findViewById(R.id.text);  
        text.setText("Hello, Welcome to Mr Wei's blog!");  
        ImageView image = (ImageView) layout.findViewById(R.id.image);  
        image.setImageResource(R.drawable.icon);  
        builder = new AlertDialog.Builder(mContext);  
        builder.setView(layout);  
        alertDialog = builder.create();  
        alertDialog.show();  
    }  
}  


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/linshutao/archive/2010/07/27/5770107.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值