Android基础知识回顾——Android常用控件

Android提供了大量的UI控件,使用这些控件可以轻松地编写出我们想要的界面,下面我们就来讲下几个比较常用的控件

TextView

  • TextView可以说是Android中最简单的控件了,它主要用来在界面上显示一些文本信息,我们来体验一下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>
  • 运行结果
    这里写图片描述
  • 其中几个属性,android:id是唯一标识符,android:layout_width和android:layout_height是控件的宽度和高度,android:text则是指定显示的文本内容

Button

  • Button也是程序与用户交互的一个重要控件,它的配置和TextView是差不多的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.yang.test04.MainActivity">

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

</LinearLayout>
  • 同样指定一个唯一标识符,将宽高设为跟随内容,接下来我们可以在MainActivity中为Button的点击事件注册一个监听器
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                在此处添加代码
            }
        });
    }
}
  • 这样当点击Button的时候,就会执行监听器中的onClick()方法,只需要在这个方法中加入要处理的逻辑就行

EditText

  • EditText允许用户在控件里输入和编辑内容,并且可以在程序中对这些内容进行处理,例如qq的聊天输入框
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.yang.test04.MainActivity">

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

</LinearLayout>
  • 就像前面的控件一样,定义一个id,指定宽高
    这里写图片描述
  • 我们就可以在里面输入内容了

ImageView

  • ImageView是用于在界面上展示图片的一个控件,我们需要准备一些图片,图片资源通常是放在以“drawable”开头的目录下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.yang.test04.MainActivity">

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

</LinearLayout>
  • 运行结果
    这里写图片描述
  • 在这里使用android:src指定了一张图片,由于图片的宽高未知,所以设置成wrap_content

ProgressBar

  • ProgressBar用于在界面上显示一个进度条,表示我们的程序正在执行一些东西,它的用法也非常简单
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.yang.test04.MainActivity">

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

</LinearLayout>
  • 运行程序后就可以看到一个圆形进度条正在旋转,我们可以通过指定android:visibility属性来控制该控件的可见性,可选值有3种:visible,invisible和gone。visible表示控件是可见的,这个是默认值,invisible表示控件不可见,但是它仍然占据着位置,gone表示控件不可见并且不占据位置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值