Android基本控件注意点

总结一些基本的注意点
  • wrap_content 表示由控件内容决定当前控件的大小

  • android:gravity 指定文字的对齐方式(表示当前View,即控件,内部的东西的,对齐方式

  • android:layout_gravity 可以指定控件的对齐方式(表示当前View,即控件本身在父一级内的(即父一级控件所给当前子控件所分配的显示范围内)的对齐方式

  • center = center_vertical + center_horizontal

  • 系统会对Button中的所有英文字母自动进行大写转换, 可通过 android:textAllCaps="false" 来禁止这一默认特性

  • android:hint 属性指定了一段提示性的文本

  • android:maxLines 指定了EditText的最大行数,这样当输入内容超过指定行数时,文本就会向上滚动,而EditText则不会再继续拉伸

  • ImageView 在xml中通过 android:src 属性指定图片;在java中通过setImageResource()方法指定图片

  • Android控件的可见属性 android:visibilty 来指定,可选值有三种:visible、invisible和gone。visible表示控件是可见的,是默认值,invisible表示控件不可见,但是它仍然占据着原来的位置和大小,gone则表示控件不仅不可见,而且不再占用任何屏幕空间

  • 在java中通过代码设置控件的可见性 setVisibility()方法,传入View.VISIBLE 、View.INVISIBLE、View.GONE

  • AlertDialog 弹出对话框

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    AlertDialog.Builder dialog = new AlertDialog.Builder(ThirdActivity.this);
    dialog.setTitle("this is a dialog");
    dialog.setMessage("Something important");
    dialog.setCancelable(false); //表示dialog不可通过BACK键来取消
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
    });
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
    });
    dialog.show();
  • ProgressDialog弹出一个对话框,并且对话框中显示一个进度条,一般用于表示当前操作比较耗时

    • Android在API27中废弃了ProgressDialog。 弃用的原因:ProgressDialog是浮现在Activity上的一层,它阻止了用户的交互,所以不友好。
    1
    2
    3
    4
    5
    ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setTitle("This is ProcessDialog");
    progressDialog.setMessage("加载中...");
    progressDialog.setCancelable(true);
    progressDialog.show();

    数据加载完成后要通过dismiss()来关闭

0%