TextView
先看下述代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is TextView"
android:textColor="#00ff00"
android:textSize="24sp" />
</LinearLayout>
TextView可以说是Android中最简单的一个控件了,它主要用于在界面上显示一段文本信息。接下来我们逐条分析:
android:id= :给当前控件定义了唯一标识符,也就是在R.java文件里新增一个id名称。定义规则是 @+id/......,但如果已经存在相同的id,就直接覆盖掉。
android:layout_width= 和 android:layout_height= :指定了控件的宽度和高度。Android中所有的控件都具有这两个属性,可选值有三种:match_parent(当前控件的大小和父布局的大小一样)、fill_parent、wrap_content(控件内容决定当前控件大小)。其中:match_parent、fill_parent的意义相同,现在官方更加推荐使用match_parent。
android:gravity= :指定文字的对齐方式,可选值有top、bottom、left等,可以用“|”来同时指定多个值,但需要注意 android:orientation 所规定的方向,假如在vertical情况下使用horizontal才能使用的如:center_horizontal,则会对实际显示效果产生影响。
android:text= :指定TextView中显示的文本内容。
android:textColor= : 指定文字的颜色。
android:textSize= : 指定文字的大小。
当然TextView中还有许多其他的属性,可以自行查阅文档。
Button
先看下述代码:
...
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textAllCaps="false" />
Button是程序用于和用户进行交互的一个重要控件。
android:textAllCaps= :我们会发现android:text="Button"在最后显示是BUTTON,全部是大写形式,系统会默认对Button中的所有英语字母自动进行大写转换,如果这不是你要的实际效果,则可以使用**android:textAllCaps="false"**来禁用这一默认特性。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//在此处添加逻辑
}
});
}
}
Button button = (Button) findViewById(R.id.button) : 通过 findViewById() 方法获取到布局文件中定义的元素,而 findViewById() 方法返回的是一个View对象,我们需要向下转型将它转成Button对象,()里是通过android:id定义。
button.setOnClickListener(new View.OnClickListener() :为按钮注册一个监听器,点击按钮时就会执行监听器中的onClick()方法,如创建一个Toast等。
EditText
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入..."
android:maxLines="2" />
android:hint= :我们往往需要在输入栏里显示一些提示性的文字,然后用户一旦输入了任何内容,这些提示性的文字就会消失,我们只需要在 android:hint="" 里输入想要显示的字即可。
android:maxLines= :不过,随着输入的内容不断增多,EditText的行数将不断增加,这样既不美观也不实用,我们需要限制它的行数,但也不用担心多余的内容无法显示,文本可以上下滚动。
ImageView
ImageView是用于在界面上展示图片的一个控件,它可以让我们的程序界面变得更加丰富多彩。我们需要事先准备好图片材料,图片通常都是放在以“drawable”开头的目录下的,我们项目中有一个“drawable”目录,不过由于这个目录没有指定具体的分辨率,所以一般不使用它来放置图片。我们在rea目录下新建一个drawable—xhdqi目录,然后将事先准备好的两张图片 jpg_1.jpg 和 jpg_2.jpg 复制到该目录当中。
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/jpg_1" />
android:src= :我们给ImageView指定了一张图片,由于图片的宽和高都是未知的,所以将ImageView的宽和高都设定为wrap_content,这样就保证了不管图片的尺寸是多少,图片都可以完整地展示出来。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image_view);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
imageView.setImageResource(R.drawable.jpg_2);
break;
default:
break;
}
}
}
在按钮的点击事件里,通过调用 ImageView的setImageResource() 方法将显示的图片改成jpg_2.