「这是我参与11月更文挑战的第7天,活动详情查看:2021最后一次更文挑战」
TextView
在界面上显示一段文本信息。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:text="This is TextView"/>
</LinearLayout>
指定文字的对齐方式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:gravity="center"
android:text="This is TextView"/>
</LinearLayout>
可以使用‘|’来同时指定gravity的多个参数。
也可以指定文字的大小(textSize,单位sp)和颜色(textColor),颜色可以用取色器去提取自己想要的颜色,然后填入16进制数据就好。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_view"
android:gravity="center"
android:textSize="20sp"
android:textColor="#00ff00"
android:text="This is TextView"/>
</LinearLayout>
Button
按钮,这是一个非常熟悉的控件,其实Button是继承自TextView,因为同样的你可以在指定Text属性。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="button"/>
</LinearLayout>
会发现button全是大写,可以通过textAllcaps属性进行修改。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:textAllCaps="false"
android:text="button"/>
</LinearLayout>
显然,button也是可以用来响应事件的。
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Click the button",Toast.LENGTH_SHORT).show();
}
});
EditText
这个控件可以和用户进行交互(当然button也可以),它允许用户在控件里输入和编辑内容,并且可以在程序中对这些内容处理。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
部分输入框还有提示功能,这里用hint属性实现。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type Something here"/>
</LinearLayout>
当输入的文本过长时,我们可以使用maxLines来指定最大显示的行数(其他的文本可以通过滑动来看到)。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:hint="Type Something here"/>
</LinearLayout>
也可以对这些数据进行一些交互,如通过Toast将信息显示出来。
Button button=findViewById(R.id.button);
EditText editText=findViewById(R.id.edit_text);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,editText.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
ImageView
imageView是一个在界面上展示图片的一个控件,图片一般放在drawable目录下,但是这个目录没有分辨率,于是可以创建一个drawable-xhdpi目录,放在这下面。 需要注意的是图片名字不能以数字开头,一般小写字母加下划线。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/cute_girl"/>
</LinearLayout>
ProgressBar
进度条也是一个常见的控件,表示我们的程序正在加载一些数据。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar"/>
</LinearLayout>
但是一般加载数据的时候,加载数据完成了进度条也就消失了,那么要怎样做到这个效果呢?,android给了我们每个控件一个visibility属性。 这个属性有三个值:
- visible(默认,表示可见)
- invisible(不可见,当时控件还在,相当于透明)
- gone(消失)
Button button=findViewById(R.id.button);
ProgressBar progressBar=findViewById(R.id.progressBar);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.INVISIBLE);
}
});
也可以改变它的style
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="finished"
android:textAllCaps="false"
android:layout_gravity="center"/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar"/>
</LinearLayout>
这样就变成了水平进度条。
还可以设置它的一个最大值,可以通过这个最大值来体现进度。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="finished"
android:textAllCaps="false"
android:layout_gravity="center"/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:id="@+id/progressBar"/>
</LinearLayout>
Button button=findViewById(R.id.button);
ProgressBar progressBar=findViewById(R.id.progressBar);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setProgress(progressBar.getProgress()+10);
}
});
AlertDialog
对话框也是一个很常见的控件,用来提示用户某些信息,当出现弹窗时Activity会处于onPause状态。 和别的不同的是,这个对话框可以在Acitivity中直接定义书写。
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建一个对话框
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
//将对话框设置为不可取消
dialog.setCancelable(false);
//设置对话框的标题
dialog.setTitle("This is dialog");
//设置对话框的内容
dialog.setMessage("Something is important");
dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
}
});
}
ProgressDialog
当需要提示用户当前进度时就可以用这个控件了,这个控件会弹出一个进度条来提示用户当前进度。使用方法和AlertDialog差不多,也可以在Activity中创建。
Button button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建一个进度弹窗
ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setTitle("this is a progressDialog");
progressDialog.setCancelable(true);
progressDialog.show();
}
});