APP开发小贴士---问题虽小,但是需要注重

来源:互联网 发布:黑暗骑士崛起 知乎 编辑:程序博客网 时间:2024/05/02 01:56

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.XMLactivity下面加入

android:configChanges=”orientation|keyboardHidden”

<uses-permissionandroid:name="android.permission.CHANGE_CONFIGURATION"/>

 

orientation =this.getResources().getConfiguration().orientation;

activity里面重载@Override

    public voidonConfigurationChanged(Configuration newConfig) {

      super.onConfigurationChanged(newConfig);

      if (orientation ==Configuration.ORIENTATION_LANDSCAPE) {

           

      }

 

      else if (orientation ==Configuration.ORIENTATION_PORTRAIT) {

                 

      }

}

---------------------------------------------------------------------------

保存上次的记录spinner

使用sharepreference

SharedPreferencessettings;

      Editor editorsettings;

public void onCreate(Bundle savedInstanceState) {

 

ArrayAdapter adapterserver =newArrayAdapter<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(newOnItemSelectedListener(){

 

      @Override

      public voidonItemSelected(AdapterView<?> parent, View arg1,

                              int postion, long id) {

editorsettings.putInt("ServerNumPosition", postion);                    editorsettings.putString("ServerNum", selectedServerName);

      editorsettings.commit();

}

}

 

----------------------------------------------------------------------------------------------------------

 

判断sdcard目录或者路径是否存在:

public voidcheckPath(String pathname) {

      try{

      StringsDStateString = android.os.Environment.getExternalStorageState();

      if(sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {

                  Filepath = new File(pathname);

                  if(!path.exists()){

                        if(path.isDirectory()){

                              path.mkdirs();  //创建目录

                              Toast.makeText(this, pathname + "hasexisted", Toast.LENGTH_LONG).show();

                        }

                        else if(path.isFile())

                        {

                              path.createNewFile();  //新建文件

                        }

                  }

            }

      }catch(Exception e)

      {

            System.out.println("errorfor " + ": "

                              + e.getMessage());

      }

}

 

原创粉丝点击