• 常见控件
    • TextView
    • Button
    • EditText
    • ImageView
    • ProgressBar
    • AlertDialog
    • ProgressDialog

常见控件

TextView

然后使用 android:layout_width 指定了控件的宽度,使用 android:layout_height 指定了控件的高度。Android 中所有的控件都具有这两个属性,可选值有三种 match_parent、fill_parent 和 wrap_content,其中 match_parent 和fill_parent 的意义相同,现在官方更加推荐使用 match_parent。match_parent 表示让当前控件的大小和父布局的大小一样

<TextView     android:id="@+id/text_view"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="This is TextView"    />

文字对齐(默认左对齐):
例如

android:gravity="center"

修改文字的大小和颜色:
例如:

android:textSize="24sp"android:textColor="#00ff00"

Button

Button的监听除了匿名类方式,还可以以实现接口的方式(implements OnclickListener)

<Button    android:id="@+id/button"   android:layout_width="match_parent"   android:layout_height="wrap_content"   android:text="Button"   />

EditText

<EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" />

添加提示文字:

android:hint="随便写"

最大行数显示限制(写多了行数会增加,因为高度是wrap的)

android:maxLines="2"

Button和EditText的配合:

 protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     button = (Button) findViewById(R.id.button);     editText = (EditText) findViewById(R.id.edit_text);     button.setOnClickListener(this); }
@Overridepublic void onClick(View v) {    // TODO Auto-generated method stub    switch(v.getId()){    case R.id.button:        //获得输入框中的字符串        String inputText = editText.getText().toString();        //吐司输出        Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_LONG).show();        break;    default:            break;    }}

ImageView

<ImageView  android:id="@+id/iamge_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" />

这里用warp是保证图片都不管什么尺寸都能显示

利用button改变显示的图片
onCreate中

imageView = (ImageView) findViewById(R.id.iamge_view);

onClick中

imageView.setImageResource(R.drawable.img);

ProgressBar

圆形的:

<ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" />

安卓控件的属性:visible、invisible、gone三种。默认都是可见的,gone表示不占用屏幕空间了,但是invisible还占用着,案例:
onCreate中:

progressBar = (ProgressBar) findViewById(R.id.progress_bar);

onClick中:

public void onClick(View v) {    // TODO Auto-generated method stub    switch(v.getId()){    case R.id.button://          String inputText = editText.getText().toString();//          Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_LONG).show();//          imageView.setImageResource(R.drawable.img);        if(progressBar.getVisibility()==View.GONE){            progressBar.setVisibility(View.VISIBLE);        }else{            progressBar.setVisibility(View.GONE);        }        break;    default:            break;    }}

长条形的Bar:

 style="?android:attr/progressBarStyleHorizontal" android:max="100"

AlertDialog

在onClick中:

    public void onClick(View v) {        // TODO Auto-generated method stub        switch(v.getId()){        case R.id.button:            AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);            dialog.setTitle("this is dialog");            dialog.setMessage("something improtant");            dialog.setCancelable(false);            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface arg0, int arg1) {                    // TODO Auto-generated method stub                }            });            dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface arg0, int arg1) {                    // TODO Auto-generated method stub                }            });            dialog.show();            break;        default:                break;        }    }

ProgressDialog

在onClick中:

@Overridepublic void onClick(View v) {    // TODO Auto-generated method stub    switch(v.getId()){    case R.id.button:        ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);        progressDialog.setTitle("This is ProgressDialog");        progressDialog.setMessage("Loading...");        //不能通过返回键取消        progressDialog.setCancelable(true);        progressDialog.show();        break;    default:            break;    }}

更多相关文章

  1. android Dialog大小修改
  2. 在Fragment中设置控件点击方法,执行失败。
  3. Android解决父控件拦截子控件手势滑动事件的问题
  4. 整理:Android自带、第三方需要添加依赖的控件
  5. Android Interface(UI) 界面控件简单介绍01
  6. android 自定义导航控件
  7. Android 控件之Gallery图片集

随机推荐

  1. Android6.0权限系统
  2. 插件化二(Android)
  3. 打造Android一体化轮播广告条
  4. Android缓存处理和清除数据、清除缓存、
  5. Android极光推送(Android(安卓)studio 3.
  6. 【Android开发学习17】Android OpenGL ES
  7. Android 当打开“开发者模式”中的“不保
  8. Android上 用Html5做界面,javascript调用
  9. Android智能硬件开发心得总结(二)
  10. Android之Zygote介绍