安卓开发实战(5)之Android常用控件

TextView显⽰⽂本

<TextView
 android:id="@+id/text_view"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:textSize="24sp"
 android:textColor="#00ff00"
 android:text="This is TextView" />

android:id :给当前控件定义了⼀个唯⼀标识符。
android:layout_width:指定控件的宽度,可选值:match_parent (fill_parent)和wrap_content ,match_parent 表⽰让当前的控件的⼤⼩和⽗
布局的⼤⼩⼀样,也就是由⽗布局来决定当前控件的⼤⼩;wrap_content表⽰让当前控件的⼤⼩能够刚好包含住⾥⾯的内容,也就是由控件
内容决定当前⼤⼩,也可以设置特定的⼤⼩。
android:layout_height:指定控件的⾼度,内容同上。
android:gravity :来指定⽂字的对齐⽅式,可选值有top、bottom、left、right、center等。
android:textSize:指定⽂字的⼤⼩。
android:textColor:指定⽂字的颜⾊。
android:text:指定TextView中的⽂本显⽰内容。

Button最常用的按钮

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

android:textAllCaps:是否将英⽂字母⾃动转换成⼤写

EditText在控件⾥输⼊和编辑内容

<EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:Layout_height="wrap_content"
    android:hint="Type something here"
    android:maxLines="2"/>

android:hint :指定了⼀段提⽰性的⽂本。
android:maxLines :指定了EditText的最⼤⾏数为两⾏,这样当输⼊的内容超过两⾏时,⽂本就会向上滚动,⽽EditText则不会再继续拉
伸。

ImageView在界⾯上展⽰图⽚,图⽚通常都是放在以“drawable”开头的⽬录下。

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

android:src :给ImageView指定了⼀张图⽚。

ProgressBar⽤于在界⾯上显⽰⼀个进度条,表⽰我们的程序正在加载⼀些数据。

<ProgressBar
 android:id="@+id/progress_bar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 style="?android:attr/progressBarStyleHorizontal"
 android:max="100"
/>

style:设置进度条样式。
android:max:设置进度条最⼤值。

AlertDialog可以在当前的界⾯弹出⼀个对话框,置顶于所有界⾯元素之上的,能屏蔽掉其他控件的交互能⼒。

@Override
public void onClick(View v) {
 switch (v.getId()) {
  case R.id.button:
   AlertDialog.Builder dialog = new AlertDialog.Builder (MainActivity.this);
   dialog.setTitle("This is Dialog");
   dialog.setMessage("Something important.");
   dialog.setCancelable(false);
   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();
   break;
  default:
   break;
  }
 }
}

ProgressDialog类似于AlertDialog,会在对话框中显⽰⼀个进度条。

@Override
public void onClick(View v) {
 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;
  }
 }
}

注意,如果在setCancelable() 中传⼊了false ,表⽰ProgressDialog是不能通过Back键取消掉的,这时你就⼀定要在代码中做好控制,当数
据加载完成后必须要调⽤ProgressDialog的dismiss() ⽅法来关闭对话框,否则ProgressDialog将会⼀直存在。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

czDemo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值