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

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

      }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值