- TextView文本框
- Toast 提示
- Button 按钮
- EditText 文本输入框
- ImageView 图片
- CheckBox 复选框
- RadioGroup/RadioButton 单选按钮组
注意:View的大小写
1.TextView文本框
TextView是用来显示文本的组件。
android:text=“XXX” 文字内容 tv.setText()
android:hint=“XXXXX” 提示内容
android:textColor="#000" 文字的颜色。 也可以=“@color/colorAccent”使用Color资源
android:textSize=“20sp” 表示文字的大小。建议字体单位为sp,默认情况下,1sp和1dp的大小是一样的
android:gravity=“center” 表示TextView中的文字相对于TextView的对齐方式。
android:background="#ccc" 表示TextView的背景颜色
android:singleLine=“true” true或者false。如果设置为true为单行输入。
android:lines="2" 不管多大都显示两行
android:ellipsize="end"
android:maxLines="2" 超过两行只显示两行
android:ellipsize="end"
(android:ellipsize = "end" 省略号在结尾
android:ellipsize = "start" 省略号在开头
android:ellipsize = "middle" 省略号在中间
android:ellipsize = "marquee" 跑马灯 EditText不支持跑马灯)
在java代码中同样可通过 ”组件名.setXXX()方法设置。如,tv.setTextColor();
2.Toast提示
Toast.LENGTH_LONG 3.5秒
Toast.LENGTH_SHORT 2秒。
Toast t = Toast.makeText(context,"happy new year", Toast.LENGTH_LONG);
t.show();
设置提示位置:Gravity.CENTER:中间
Gravity.BOTTOM:下方
Gravity.TOP:上方
Gravity.RIGHT: 右边
Gravity.LEFT:左边
Toast toast = Toast.makeText(getApplicationContext(), "点击按钮", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
自定义Toast:
Toast toast2 = new Toast(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.toast_custom, null);
toast2.setView(view);
toast2.setGravity(Gravity.CENTER, 0, 0);
toast2.show();
3.Button按钮
Button是Android中一个非常简单的控件,在我们平时的项目中,可以说是非常的常见,使用率也是相当高。常用属性:
android:text //设置显示的文本
android:textColor //设置显示文本的颜色
android:textSize //设置显示文本字体大小
android:background //可拉伸使用的背景
4.EditText 文本输入框
大致属性与TextView相同
android:drawableLeft="@drawable/你的图片ID"
5.ImageView 图片
1)background指的是背景,foreground指的是前景,而src指的是内容;三者可以同时使用;
2)src填入图片时,是按照图片大小直接填充,并不会进行拉伸;而使用background和foreground填入图片,则是会根据ImageView给定的宽度来进行拉伸;
3)background和foreground是所有view都有的属性,总是缩放到view的大小,不受scaleType影响;而src是ImageView特有属性,它会受到scaleType的影响。
4)宽高等比自适应,但是宽高不同src不会拉伸,background会拉伸
6.CheckBox 复选框
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cbx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="游泳"
/>
在activity中:setOnCheckedChangeListener
7.RadioGroup/RadioButton 单选按钮组
RadioGroup单选按钮组/RadioButton单选按钮
<RadioGroup
android:id="@+id/rg_Orientation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp">
<RadioButton
android:id="@+id/rb_Portrait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:button="@null" //去掉圆圈位置
android:drawableLeft="@drawable/XXXX"
/>
<RadioButton
android:id="@+id/rb_Landscape"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioGroup>
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio0:
Toast.makeText(MainActivity.this, "男", 1).show();
break;
case R.id.radio1:
Toast.makeText(MainActivity.this, "女", 1).show();
break;
}