文章目录
一、文本显示
1.设置文本的内容
设置文本内容有两种方式:
在 XML 文件中通过属性 android:text 设置文本
<TextView
....
....
android:text="你好,世界!"/>
在 Java 代码中调用文本视图对象的 setText 方法设置文本
//根据项目工程文件会自动生成R这一个类,这个类可能包含文字还有图片
setContentView(R.layout.activity_text_view);
TextView tv_hello=findViewById(R.id.tv_hello);
tv_hello.setText("你好,世界!");
也可以在value中的String.xml中设置字体,之后再在xml和java代码中引用。
<string name="hello">你好,世界!</string>
在XML文件中引用(@string/***)
<TextView
....
....
android:text="@string/hello"/>
在Java代码中引用(R.string.***)
//根据项目工程文件会自动生成R这一个类,这个类可能包含文字还有图片
setContentView(R.layout.activity_text_view);
TextView tv_hello=findViewById(R.id.tv_hello);
tv_hello.setText(R.string.hello);
2.设置文本的大小
XML中要求在字号数字后面写明单位类型,常见的字号单位主要有px,dp,sp三种。
1.px:指像素,是指基本原色素及其灰度的基本编码。
px是手机屏幕的最小显示单位,它与设备的显示屏有关。
2.dp:安卓开发时的长度单位。
dp有时也写作dip,指的是与设备无关的显示单位,它只与屏幕的尺寸有关。
对于尺寸相同的手机,即使分辨率不同,同dp的组件占用屏幕比例也相同。dp的UI效果只在相同尺寸的屏幕上相同,如果屏幕尺寸差异过大,那么需要重做DP适配,这也就是为什么平板需要单独做适配的原因。
3.sp:与缩放无关的抽象像素,是字体单位。一般情况下可认为sp=dp。
它专门 用来设置字体大小。手机在系统设置里可以调整字体的大小(小、标准、大、超大)。
补充:
名称 | 解释 |
---|---|
px(Pixel像素) | 也称为图像元素,是作为图像构成的基本单元,单个像素的大小并不固定,跟随屏幕大小和像素数量的关系变化,一个像素点为1px。 |
Resolution(分辨率) | 是指屏幕的垂直和水平方向的像素数量,如果分辨率是 1920*1080 ,那就是垂直方向有 1920 个像素,水平方向有 1080 个像素。 |
Dpi(像素密度) | 是指屏幕上每英寸(1英寸 = 2.54 厘米)距离中有多少个像素点。/td> |
Density(密度) | 是指屏幕上每平方英寸(2.54 ^ 2 平方厘米)中含有的像素点数量。/td> |
Dip / dp (设备独立像素) | 也可以叫做dp,长度单位,同一个单位在不同的设备上有不同的显示效果,具体效果根据设备的密度有关,详细的公式请看下面 。 |
像素之间的转换:
3.设置文本的颜色
在java里调用setTextColor方法可以设置文本颜色,color类中的颜色如下:
颜色类型 | 说明 |
---|---|
BLACK | 黑色 |
DKGRAY | 深灰 |
GRAY | 灰色 |
LTGRAY | 浅灰 |
WHITE | 白色 |
RED | 红色 |
GREEN | 绿色 |
BLUE | 蓝色 |
YELLOW | 黄色 |
CYAN | 青色 |
MAGENTA | 玫瑰红 |
TRANSPARENT | 透明 |
在XML文件中则通过属性android:textColor指定文本颜色
RGB三原色(红色red、绿色green、蓝色blue)
RGB色值有按八位十六进制数编码和六位十六进制数编码方式。
在八位编码中,头两位表示透明度,接下来为红色浓度,再就是绿色浓度,最后是蓝色浓度。
比如:00123456
00两位表示透明度
12两位为红色浓度
34两位为绿色浓度
56两位为蓝色浓度
全0颜色就越淡,全1颜色就越深,也就是说当首两位为FF时,表示不透明,为00则为透明,其余的类似。
注意:不透明才能看得见颜色,设置为透明,即使其他位上有颜色也显示不出来。
在java中通过setTextColor方法设置6位(即不设置头两位),是默认透明的。
在xml文件中不设置头两位,是默认不透明的。
具体在java代码和xml中引用与上面设置字的大小类似
在XML文件中引用(@color/xxx)。
在Java代码中引用(R.color.xxx)。
背景色设置也类似。
二、视图基础
1.设置视图的宽高
android:layout_width:宽度
android:layout_height:高度
宽高的取值主要有下列三种:
match_parent:表示与上级视图保持一致,继承父类的值。
wrap_content:表示与内容自适应。
以dp为单位的具体尺寸。
最后效果:
2.设置视图的间距
有两种方式,分别是使用layout_margin属性和使用padding属性,
设置视图的对齐方式。
layout_margin:指定了当前视图与周围平级视图之间的距离。也就是外间距,具体包含上下左右。
padding:指定了当前视图与内部下级视图之间的距离。也就是内间距,也包含上下左右。
<!--最外层的布局背景为蓝色 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00AAFF"
android:orientation="vertical">
<!--中间的布局背景为黄色-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF99"
android:layout_margin="20dp"
android:padding="60dp"
>
<!--最內層的视图背景为红色 -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"/>
</LinearLayout>
3.设置视图的对齐方式
对齐方式也有两种途径,使用layout_gravity属性和使用gravity属性。
layout_gravity:指定了当前视图相对于父类视图的对齐方式。
gravity:指定了子类视图相对于当前视图的对齐方式。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#ffff99"
android:orientation="horizontal">
<!--第一个子布局背景为绿色,它在上级视图中朝下对齐,它的下级视图则靠左对齐-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:padding="10dp"
android:background="#00ff00"
android:layout_gravity="bottom"
android:gravity="left">
<!-- 内部视图的宽度和高度都是100dp,且背景色为青色 -->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff"/>
</LinearLayout>
<!-- 第二个子布局背景为绿色,它在上级视图中朝上对齐,它的下级视图则靠右对齐 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:padding="10dp"
android:background="#00ff00"
android:layout_gravity="top"
android:gravity="right">
<!-- 内部视图的宽度和高度都是100dp,且背景色为青色 -->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff"/>
</LinearLayout>
</LinearLayout>
三、常用布局
1.线性布局LinearLayout
线性布局内部有两种排列方式,分别为水平和垂直。在orientation标签有两种数学分别是horizontal和vertical。标签内如果不指定排列方式的话,默认排列方式为vertical。
horizontal:内部视图在水平方向从左往右排列。
vertical:内部视图在垂直方向从上往下排列。
线性布局的权重layout_weight,不在LinearLayout节点设置,而在线性布局的直接下级视图设置,表示该下级视图占据的宽高比例。
layout_width标签属性填0,表示水平方向的宽度比例。
layout_height标签属性填0,表示垂直方向的宽度比例。
标签layout_weight填=数字,表示在同一视图下所占的比例。
<?xml version="1.0" encoding="utf-8"?>
<!--总布局为垂直布局-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!--水平方向排列-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="横排第一个"
android:textSize="20sp"
android:textColor="@color/red"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="横排第二个"
android:textSize="20sp"
android:textColor="#ff00ff00"
/>
</LinearLayout>
<!--垂直方向排列-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="竖排第一个"
android:textSize="20sp"
android:textColor="@color/red"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="竖排第二个"
android:textSize="20sp"
android:textColor="#ff123586"
/>
</LinearLayout>
</LinearLayout>
2.相对布局RelativeLayout
相对布局的下级视图位置由其他视图决定。定位参照物有两种:
同级视图
父级视图(也就是它归属的RelativeLayout)。如果不设置参照物,那么下级视图默认显示在RelativeLayout内的左上角。
相对布局的取值如下:
相对位置的属性取值 | 说明 |
---|---|
layout_toLeftOf | 当前视图在指定视图的左边 |
layout_toRightOf | 当前视图在指定视图的右边 |
layout_above | 当前视图在指定视图的上方 |
layout_below | 当前视图在指定视图的下方 |
layout_alignLeft | 当前视图与指定视图的左侧对齐 |
layout_alignRight | 当前视图与指定视图的右侧对齐 |
layout_alignTop | 当前视图与指定视图的顶部对齐 |
layout_alignBottom | 当前视图与指定视图的底部对齐 |
layout_centerInParent | 当前视图在上级视图中间 |
layout_centerHorizontal | 当前视图在上级视图的水平方向居中 |
layout_centerVertical | 当前视图在上级视图的垂直方向居中 |
layout_alignParentLeft | 当前视图与上级视图的左侧对齐 |
layout_alignParentRight | 当前视图与上级视图的右侧对齐 |
layout_alignParentTop | 当前视图与上级视图的顶部对齐 |
layout_alignParentBottom | 当前视图与上级视图的底部对齐 |
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:id="@+id/tv_center"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_centerInParent="true"
android:text="我在中间"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_center_horizontal"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_centerHorizontal="true"
android:text="我在水平中间"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_center_vertical"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_centerVertical="true"
android:text="我在垂直中间"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_parent_left"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_alignParentLeft="true"
android:text="我在上级的左边"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_parent_right"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_alignParentRight="true"
android:text="我在上级的右边"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_parent_top"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_alignParentTop="true"
android:text="我在上级的顶部"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_parent_bottom"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_alignParentBottom="true"
android:text="我在上级的底部"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_left"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_toLeftOf="@id/tv_center"
android:layout_alignTop="@id/tv_center"
android:text="我在中间左边"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_right_center"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_toRightOf="@id/tv_center"
android:layout_alignBottom="@id/tv_center"
android:text="我在中间左边"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_above_center"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_above="@id/tv_center"
android:layout_alignLeft="@id/tv_center"
android:text="我在中间上边"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_below_center"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_below="@id/tv_center"
android:layout_alignRight="@id/tv_center"
android:text="我在中间下边"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"/>
</RelativeLayout>
3.网格布局GridLayout
顾名思义,网格布局支持多行多列的表格排列,默认从左往右、从上到下排列,它有两个属性,分别是行和列:
columnCount:指定了网格的列数,即每行能放多少个视图;
rowCount:指定了网格的行数,即每列能放多少个视图;
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2">
<TextView
android:gravity="center"
android:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#ffcccc"
android:text="浅红色"
android:textColor="#000000"
android:textSize="18sp">
</TextView>
<TextView
android:layout_columnWeight="1"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#ffaa00"
android:text="橙色"
android:textColor="#000000"
android:textSize="18sp">
</TextView>
<TextView
android:layout_columnWeight="1"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#00ff00"
android:text="绿色"
android:textColor="#000000"
android:textSize="18sp">
</TextView>
<TextView
android:layout_columnWeight="1"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#660066"
android:text="深紫色"
android:textColor="#000000"
android:textSize="18sp">
</TextView>
</GridLayout>
4.滚动视图ScrollView
滚动视图也有两个属性,分别是水平和垂直的滚动视图,ScrollView和HorizontalScrollView。
采用的视图 | layout_width属性 | layout_height属性 |
---|---|---|
ScrollView | match_parent | wrap_content |
HorizontalScrollView | wrap_content | match_parent |
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 水平方向的滚动视图,当前高度为200dp -->
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="200dp">
<!--水平方向的线性布局,两个子视图的颜色分别为青色和黄色-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="#aaffff" />
<View
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="#ffff00"/>
</LinearLayout>
</HorizontalScrollView>
<!-- 垂直方向的滚动视图,当前高度自适应,宽度继承父级 -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 垂直方向的线性布局,两个子视图的颜色分别为绿色和橙色 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#00ff00" />
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#ffffaa"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
四、按钮触控
1.按钮控件Button
Button由TextView派生而来,Button与TextView区别如下:
区别 | Button | TextView |
---|---|---|
默认背景 | 按钮背景 | 无背景 |
内部文本默认对齐方式 | 居中对齐 | 左对齐 |
默认是否将英文转大写 | 是 | 否 |
并且Button在TextView的基础上新增了两个属性:
textAllCaps:是否将英文字母转为大写(默认为TRUE)
onClick:接管用户的点击动作,指定了点击按钮时要触发哪个方法
public class DateUtil {
public static String getNowTime(){
SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());
}
}
public void doClick(View view){
String desc=String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)view).getText());
tv_result.setText(desc);
}
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面的按钮英文默认大写"
android:textColor="@color/black"
android:textSize="18sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="@color/black"
android:textSize="18sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面的按钮英文保持原状"
android:gravity="center"
android:textColor="@color/black"
android:textSize="18sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:textAllCaps="false"
android:textColor="@color/black"
android:textSize="18sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="直接指点击方法"
android:textColor="@color/black"
android:textSize="18sp"
android:onClick="doClick"
/>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这里查看按钮的点击结果"
android:textColor="@color/black"
android:textSize="18sp"
/>
运行结果:
2.点击事件和长按事件
监听器,专门监听控件的动作行为。只有控件发生了指定的动作,监听器才会触发开关去执行对应的代码逻辑。所以Button中有两种常用的监听器,分别是点击监听器和长按监听器。
点击监听器:通过setOnClickListener方法设置。按钮被按住少于500毫秒时触发点击事件。
public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener{
private TextView tv_result1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_click);
tv_result1 =findViewById(R.id.tv_result1);
Button btn_click_single =findViewById(R.id.btn_click_single);
btn_click_single.setOnClickListener(new MyOnClickListener(tv_result1)) ;
Button btn_click_public =findViewById(R.id.btn_click_public);
btn_click_public.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn_click_public){
String desc=String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)v).getText());
tv_result1.setText(desc);
}
}
//写成静态内部类可以防止内存泄露
static class MyOnClickListener implements View.OnClickListener {
private final TextView tv_result1;
public MyOnClickListener(TextView tv_result1) {
this.tv_result1=tv_result1;
}
@Override
public void onClick(View view) {
String desc=String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)view).getText());
tv_result1.setText(desc);
}
}
}
对应的xml文件配置:
<Button
android:id="@+id/btn_click_single"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="指定单独的点击监听器"
android:textColor="@color/black"
android:textSize="18sp"
/>
<Button
android:id="@+id/btn_click_public"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="指定公共的点击监听器"
android:textColor="@color/black"
android:textSize="18sp"
/>
<TextView
android:id="@+id/tv_result1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:textColor="@color/black"
android:text="这里查看按钮的点击结果"
android:textSize="18sp"
/>
运行结果:
长按监听器:通过setOnLongClickListener方法设置。按钮被按住超过500毫秒时触发长按事件。
public class ButtonLongActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_long);
Button btn_long_click = findViewById(R.id.btn_long_click);
TextView tv_result0= findViewById(R.id.tv_result0);
//通过匿名内部类的方式
btn_long_click.setOnLongClickListener(v -> {
String desc=String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)v).getText());
tv_result0.setText(desc);
return false;
});
}
}
对应的xml配置文件:
<Button
android:id="@+id/btn_long_click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="指定长按的点击监听器"
android:textColor="@color/black"
android:textSize="18sp"
/>
<TextView
android:id="@+id/tv_result0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:textColor="@color/black"
android:text="这里查看按钮的点击结果"
android:textSize="18sp"
/>
运行结果:
3.禁用与恢复按钮
在实际业务开发中,按钮通常拥有两种状态,即不可用状态与可用状态,它们在外观和功能上的区别如下:
区别 | 可用按钮 | 不可用按钮 |
---|---|---|
是否允许点击 | 是 | 否 |
按钮文字颜色 | 黑色 | 灰色 |
是否允许点击由enabled属性控制,true—>允许,false—>不允许
public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn_test;
private TextView tv_res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_enable);
Button btn_enable = findViewById(R.id.btn_enable);
Button btn_disable = findViewById(R.id.btn_disable);
btn_test = findViewById(R.id.btn_test);
tv_res = findViewById(R.id.tv_res);
btn_enable.setOnClickListener(this);
btn_disable.setOnClickListener(this);
btn_test.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_enable:
//启用当前控件
btn_test.setEnabled(true);
//设置按钮的文字颜色
btn_test.setTextColor(Color.BLACK);
break;
case R.id.btn_disable:able:
//禁用当前控件
btn_test.setEnabled(false);
//设置按钮的文字颜色
btn_test.setTextColor(Color.GRAY);
break;
case R.id.btn_test:
String desc=String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(),((Button)v).getText());
tv_res.setText(desc);
break;
}
}
}
xml配置文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!--第一个按钮的宽为0,权重设置为1,将于另外一个按钮平均共享第一个线性布局-->
<Button
android:id="@+id/btn_enable"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/black"
android:text="启用测试按钮"/>
<Button
android:id="@+id/btn_disable"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/black"
android:text="禁用测试按钮"/>
</LinearLayout>
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#888888"
android:enabled="false"
android:text="测试按钮"/>
<TextView
android:id="@+id/tv_res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/black"
android:text="这里查看测试按钮的点击结果"/>
</LinearLayout>
运行结果:
五、图像显示
1.图像视图ImageView
图像视图展示的图片通常位于res/drawable目录,显示图片目前有两种方式:通过xml配置或者通过java中setImageResource方法设置。
ImageView中scaleType属性取值类型如下:
XML中的缩放类型 | ScaleType类中的缩放类型 | 说明 |
---|---|---|
fitXY | FIT_XY | 拉伸图片使其正好填满视图(图片可能被拉伸变形) |
fitStart | FIT_START | 保持宽高比例,拉伸图片使其位于视图上方或左侧 |
fitCenter | FIT_CENTER | 保持宽高比例,拉伸图片使其位于视图中间 |
fitEnd | FIT_END | 保持宽高比例,拉伸图片使其位于视图下方或右侧 |
center | CENTER | 保持图片原尺寸,并使其位于视图中间 |
centerCrop | CENTER_CROP | 拉伸图片使其充满视图,并位于视图中间 |
centerInside | CENTER_INSIDE | 保持宽高比例,缩小图片使之位于视图中间(只缩小不放大) |
默认采用fitCenter
fitCenter和center以及centerInside看起来相似,但是这三个有很大的区别,区别如下:
属性名 | 区别 |
---|---|
fitCenter | 既允许缩小图片、也允许放大图片(可大可小) |
centerInside | 只允许缩小图片、不允许放大图片(可小不可大) |
center | 自始至终保持原始尺寸(不可大也不可小) |
所以,当显示图片尺寸大于视图宽高,centerInside与fitCenter都会缩小图片,此时它们显示效果相同;当图片尺寸小于视图宽高,centerInside与center都保持图片大小不变,此时它俩的显示效果相同。
实现效果:
2.图像按钮ImageButton
顾名思义,ImageButton是带有图片的图像按钮,ImageButton继承ImageView而不是Button,它跟Button之前的区别如下:
区别 | ImageButton | Button |
---|---|---|
显示图片与文本 | 只可图像 | 都可 |
图像缩放形式 | 可按比例缩放 | 通过背景设置的图像会拉伸变形。 |
背景和前景是否可以添加图片 | 可 | 不可 |
ImageButton解决有些字符打不出来,可以用图像替换的问题,比如开平方的符号等。
ImageButton和ImageView的区别如下:
区别 | ImageButton | ImageView |
---|---|---|
是否有默认背景 | 是 | 否 |
默认缩放类型 | center | fitCenter |
3.同时展示文本与图像
实现方法大致分为两种:
第一种在LinearLayout布局中对ImageView和TextView组合布局。
第二种通过按钮控件Button的drawable属性设置文本周围的图标。
Button的drawable属性如下表所示:
属性名 | 说明 |
---|---|
drawableTop | 指定文字上方的图片 |
drawableBottom | 指定文字下方的图片 |
drawableLeft | 指定文字左边的图片 |
drawableRight | 指定文字右边的图片 |
drawablePadding | 指定图片与文字的间距 |
xml配置文件:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="图标在左"
android:drawableLeft="@drawable/ic_about"
android:drawablePadding="5dp"
/>
<Button
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="图标在右"
android:drawableRight="@drawable/ic_about"
android:drawablePadding="5dp"
/>
显示效果:
参考视频资料:哔哩哔哩动脑学院安卓从入门到项目实战