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