Android自定义对话框的大小
在 Android做界面时要弹出对话框让用户输入内容,经常遇到开始的时候没有内容对话框一点点,看起来很别扭,查了下资料,修改对话框的 WindowManager.LayoutParams可以达到修改对话框大小的目的 .
从 Dialog继承一个自定义对话框类,在其构造函数中加上如下代码:
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay(); //为获取屏幕宽、高
LayoutParams p = getWindow().getAttributes(); //获取对话框当前的参数值
p.height = (int) (d.getHeight() * 0.6); //高度设置为屏幕的 0.6
p.width = (int) (d.getWidth() * 0.95); //宽度设置为屏幕的 0.95
getWindow().setAttributes(p); //设置生效
直接创建 dialog对象例子代码 :
dialog = new Dialog( this ,R.style. FullHeightDialog );
dialog .setContentView(R.layout. transferdialog );
WindowManager m = dialog .getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
dialog .getWindow().getAttributes(). height = ( int ) (d.getHeight() * 0.6);
dialog .getWindow().getAttributes(). width = ( int ) (d.getWidth() * 0.9);
dialog .getWindow().setAttributes( dialog .getWindow().getAttributes());
---------------------------------------------------------------------
小贴士 : 若是屏幕旋转时 ,会重新执行 onCreat().修改的办法就是在 ANDROIDMAINFEST.XML的 activity下面加入
android:configChanges=”orientation|keyboardHidden”
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
orientation = this.getResources().getConfiguration().orientation;
在 activity里面重载 @Override
public void onConfigurationChanged(Configuration newConfig) {
super .onConfigurationChanged(newConfig);
if ( orientation == Configuration. ORIENTATION_LANDSCAPE ) {
}
else if ( orientation == Configuration. ORIENTATION_PORTRAIT ) {
}
}
---------------------------------------------------------------------------
保存上次的记录 spinner
使用 sharepreference
SharedPreferences settings ;
Editor editorsettings ;
public void onCreate(Bundle savedInstanceState) {
ArrayAdapter adapterserver = new ArrayAdapter<String>(
this , android.R.layout. simple_spinner_item , keyServers );
adapterserver.setDropDownViewResource(android.R.layout. simple_spinner_dropdown_item );
spinner.setAdapter(adapterserver);
settings = getSharedPreferences( "preferences_settings" ,Context. MODE_PRIVATE );
editorsettings = settings .edit();
spinner.setSelection(settings.getInt(" ServerNumPosition ", 0)); //line
spinner.setOnItemSelectedListener( new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int postion, long id) {
editorsettings .putInt( "ServerNumPosition" , postion); editorsettings .putString( "ServerNum" , selectedServerName );
editorsettings .commit();
}
}
----------------------------------------------------------------------------------------------------------
判断 sdcard目录或者路径是否存在 :
public void checkPath(String pathname) {
try {
String sDStateString = android.os.Environment.getExternalStorageState ();
if (sDStateString.equals(android.os.Environment. MEDIA_MOUNTED )) {
File path = new File(pathname);
if (!path.exists()){
if (path.isDirectory()){
path.mkdirs(); // 创建目录
Toast.makeText ( this , pathname + "has existed" , Toast. LENGTH_LONG ).show();
}
else if (path.isFile())
{
path.createNewFile(); // 新建文件
}
}
}
} catch (Exception e)
{
System. out .println( "error for " + ": "
+ e.getMessage());
}
}