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