此文章仅为本人在学习安卓开发时的一些笔记
界面显示逻辑处理
使用XML标记、描绘应用界面,使用Java书写程序逻辑
使用XML描述APP界面
使用XML文件描述APP界面
一个界面布局可以被多处代码复用,反过来,一个Java代码也可能适配多个界面布局。
解析:
Linearlayout 线性布局就是它的根节点
xmlns(xml name space):命名空间,只能使用命名空间内有的属性,使用其他的会报错
可以先看到进行修改参数的部分都是处于标签的属性中而不是标签之间
android:Layout_width="match_parent" 填充父容器,与屏幕宽高一致
android:Layout_height="match_parent"
android:orientation="vertical" 指定线性方向(垂直)
android:gravity="center"文本居中
子控件:文本控件
<TextView
android:id="@+id/tv" id名称
android:layout_width="wrap_content"
android:layout_height="wrap_content" 宽高包裹文本
android:text="PureWhite" 文本内容 />
使用Java设置程序逻辑
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RSK2HNF6-1652608917730)(https://oss.ilil.tech/chunbai_Pic/image-20220425163606899.png)]
onCreate是Activity中最先调用的方法
Tips:文本的内容既可以在layout中直接输入,也可以在res的string中进行储存和引用@string/button1
APP页面创建
- 在layout中创建xml文件,文件内内容与其他一致
- 在Java目录里创建新MainActivity(java class文件)
- 内容与其他一致
- 在AndroidManifest.xml(清单文件)中加入新创建的Activity类
在java目录里的com.example.myapplication使用新建中的Activity—>empty等,可以直接在layout、清单里自动创建添加
页面跳转(按钮)
在layout文件中创建button
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1"/>
在java文件中的onCreate方法里书写逻辑
Button button1 = findViewById(R.id.button1);//创建Button类的对象指向按钮的id
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {//按钮点击后的操作
Intent intent1 = new Intent();//创建新的意图对象
intent1.setClass(MainActivity.this,MainActivity2.class);
//确定上下文,以及要跳转的Activity
startActivity(intent1);
//启动
}
运行效果
控件在安卓中的比例很大
简单、中级、高级、自定义
在开发中,页面布局和逻辑是独立开的,因此一个页面布局可以给多个逻辑使用,同时逻辑使用也可以给多个布局使用。
简单控件
文本显示
设置文本内容
●在XML文件中通过属性android:text在根节点中设置文本
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text1"/>
其中的 android:text= 有两种写法,一种是在xml中直接定义,另一种是在res资源库中引用文本(如上方代码块) 按需求来使用。
<string name="app_name">My APP TEST</string>
<string name="text2">activity_main2</string>
<string name="text1">activity_main</string>
<string name="button1">跳转</string>
●在Java代码中调用文本视图对象的setText方法设置文本
TextView tv=findViewById(R.id.tv);//组件库会自动引入寻找对应ID
tv.setText("纯白");//通过ID来设置文本
设置文本大小
- 在XML中设置文本大小(需要指定单位)
Android屏幕尺寸详解_雪舞飞影的博客-CSDN博客_安卓手机尺寸大小
Android设计尺寸规范-优优教程网 (uiiiuiii.com)
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="@string/text1"/>
同DP:
相同尺寸,不同分辨率,组件占用屏幕的比例相同
相同分辨率,不同尺寸,组件占用屏幕的比例不同,尺寸越大,占比例越小
因此需要给不同的(过大的)手机尺寸做适配
sp会跟随系统字体大小的变化而变化
- 在JAVA中设置文本大小(默认为SP,官方推荐)
TextView tv=findViewById(R.id.tv);//组件库会自动引入
tv.setText("纯白");
tv.setTextSize(30);
文本的颜色
在XML文件中则通过属性android:textColor指定文本颜色,色值由透明度alpha和RGB三原色(红色red、绿色green、蓝色blue)联合定义。
色值有八位十六进制数与六位十六进制数两种表达方式,例如八位编码FFEEDDCC中,F表示透明度,EE表示红色的浓度,DD表示绿色的浓度,CC表示蓝色的浓度。透明度为FF表示完全不透明,为O0表示完全透明。RGB三色的数值越大,表示颜色越浓,也就越亮;数值越小,表示颜色越淡,也就越暗。
java:
TextView tv=findViewById(R.id.tv);
tv.setTextColor(Color.BLUE);//系统自带颜色
tv.setTextColor(0xff00ff00);//绿色 八位十六进制(高2位为透明度),六位十六进制(高2位默认为00,透明)
tv.setBackgroundResource(R.color.purple_700);//使用资源库中的颜色
tv.setBackgroundColor(Color.YELLOW);//设置背景色,与上同理
xml:
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text2"
android:textColor="@color/purple_200"
或者android:textColor="#ff00ff00"不透明高两位为ff或者高两位默认不透明
android:background="@color/purple_200"/>设置背景色
使用资源库中的颜色与使用RGB数 优先显示java的设定
视图显示
视图宽高
通过android:Layout_width
和android:Layout_height
等来设置,主要有:match表示与上级视图保持一致(填充父容器);wrap content:表示与内容自适应;以dp为单位的具体尺寸。
java中
- 调用控件对象的getLayoutParams方法,获取该控件的布局参数。
- 布局参数的width属性表示宽度,height属性表示高度,修改这两个属性值。
- 调用控件对象的setLayoutParams:方法,填入修改后的布局参数使之生效。
视图间距
外间距:当前视图和周围平级视图的距离,使用layout_margin属性
内间距:当前视图和内部下级视图的距离,使用padding属性
在xml布局文件中设置
视图对齐方式
xml中
常用布局
线性布局
<?xml version="1.0" encoding="utf-8"?>
<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="vertical"
tools:context=".LinerLayoutActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/teal_200"
android:gravity="center"
android:text="横排第一个"
android:textSize="30dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/teal_200"
android:gravity="center"
android:text="横排第二个"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="竖排第一个"
android:textSize="30dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="竖排第二个"
android:textSize="30dp" />
</LinearLayout>
</LinearLayout>
权重(android:layout_weight)
除了方向之外,线性布局还有一个权重概念,所谓权重,指的是线性布局的下级视图各自拥有多大比例的宽高。比如一块蛋糕分给两个人吃,可能两人平均分,也可能甲分三分之一,乙分三分之二。两人平均分的话,先把蛋糕切两半,然后甲分到一半,乙分到另一半,此时甲乙的权重比为1:1。甲分三分之一、乙分三分之二的话,先把蛋糕平均切成三块,然后甲分到一块,乙分到两块,此时甲乙的权重比为1:2。就线性布局而言,它自身的尺寸相当于一整块蛋糕,它的下级视图们一起来分这个尺寸蛋糕,有的视图分得多,有的视图分得少。分多分少全凭每个视图分到了多大的权重,这个权重在XML文件中通过属性android:layout_weight来表达。
把线性布局看作蛋糕的话,分蛋糕的甲乙两人就相当于线性布局的下级视图。假设线性布局平均分为左右两块,则甲视图和乙视图的权重比为1:1,意味着两个下级视图的layout_weight/属性都是1。不过视图有宽高两个方向,系统怎知layout_weight表示哪个方向的权重呢?所以这里有个规定,一旦设置了layout_weight属性值,便要求layout_width填Odp或者layout_height填odp。如果layout_width填0dp,则layout_weight表示水平方向的权重,下级视图会从左往右分割线性布局;如果layout_heigh填0dp,则layout_weight表示垂直方向的权重,下级视图会从上往下分割线性布局。
相对布局
相对布局(RelativeLayout)就是布局内的视图位置与其他视图有关,参照物有:
- 与该视图平级的视图
- 该视图的上级视图
使用方式:
android:layout_centerInParent="true"
<?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="400dp"
tools:context=".RelativeLayoutActivity">
<TextView
android:id="@+id/zhongjian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我在中间"
android:textColor="@color/purple_200"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="我在左上"
android:textColor="@color/purple_200"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:text="我在右中"
android:textColor="@color/purple_200"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/zhongjian"
android:layout_alignTop="@id/zhongjian"
android:text="我在中间的左边"
android:textColor="@color/purple_200"
android:textSize="20dp" />
</RelativeLayout>
网格布局
网格布局(GridLayout)支持多行多列的表格式排列,默认从左往右、从上到下
新增两个属性
- columnCount属性,它指定了网格的列数,即每行能放多少个视图;
- rowCount属性,它指定了网格的行数,即每列能放多少个视图;
<?xml version="1.0" encoding="utf-8"?>
<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="400dp"
android:columnCount="2"
android:rowCount="2"
tools:context=".GridLayoutActivity">
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="2"
android:layout_columnWeight="1"
android:gravity="center"
android:text="红色"
android:textSize="30dp" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="0"
android:layout_rowWeight="2"
android:layout_columnWeight="1"
android:gravity="center"
android:text="橙色"
android:textSize="30dp" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:gravity="center"
android:text="粉色"
android:textSize="30dp" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:gravity="center"
android:text="绿色"
android:textSize="30dp" />
</GridLayout>
android:layout_rowWeight=“2” 为行高权重
android:layout_columnWeight=“1” 为列宽权重
滚动布局
HorizontalScrollView:横向滚动布局
ScrollView:纵向滚动布局
滚动布局中,沿滚动方向的一侧为包裹内容,在滚动布局中的子组件的大小大于父容器则会使其手动滚动显示。
<?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"
tools:context=".ScrollViewActivity">
<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="400dp"
android:layout_height="match_parent"
android:background="@color/purple_200" />
<View
android:layout_width="400dp"
android:layout_height="match_parent"
android:background="@color/teal_200" />
</LinearLayout>
</HorizontalScrollView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="800dp"
android:background="@color/purple_200" />
<View
android:layout_width="match_parent"
android:layout_height="800dp"
android:background="@color/teal_200" />
</LinearLayout>
</ScrollView>
</LinearLayout>
按钮触控
Button组件
按钮控件Button由TextView派生而来,它们之间的区别有:
- Button拥有默认的按钮背景,而TextView默认无背景;
- Button的内部文本默认居中对齐,而TextView的内部文本默认靠左对齐;
- Button会默认将英文字母转为大写,而TextView保持原始的英文大小写;
Button独有的属性
- textAllCaps属性,它指定了是否将英文字母转为大写,为true是表示自动转为大写,为false表示不做大写转换。
- onClick属性,它用来接管用户的点击动作,指定了点击按钮时要触发哪个方法;(此属性较为过时,但依旧能用)
因为onClick在xml中调用了java方法,出现高耦合的情况,而软件设计中要提高复用性。应该要高内聚低耦合。
alt+enter自动导包;control+alt+f自动提取局部变量为全局变量;
<?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:gravity="center"
android:orientation="vertical"
tools:context=".ButtonTestActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="这是一段文字" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is a button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="this is a button"
android:textAllCaps="false" />
</LinearLayout>
onClick演示:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="请点击进行测试" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="查看结果"
android:textColor="@color/black" />
package com.example.myapplication;
import java.text.SimpleDateFormat;
import java.util.Date;
//新创建一个类用于获取时间
public class DataUtil {
public static String getNowtime(){
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());//把当前时间格式化,格式可查SimpleDateFormat
}
}
//Activity文件中
public void doClick(View view){
String desc = String.format("%s 您点击了按钮:%s", DataUtil.getNowtime(),((Button)view).getText());
//字符串拼接
tv_result.setText(desc);
}
实际效果:
点击与长按事件
监听器:专门监听控件的动作行为。只有控件发生了指定的动作,监听器才会触发开关去执行对应的代码逻辑。
按钮控件有两种常用监听器:
- 点击监听器:通过setOnClickListener方法设置。按钮被按住少于S00毫秒时,会触发点击事件。
- 长按监听器:通过setOnLongClickListener方法设置。按钮被按住超过500毫秒时,会触发长按事件。
点击监听
单独设置监听器
alt+inster 快速创建构造方法
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ButtonClickActivity extends AppCompatActivity {
//设置全局变量 l+alt+f
private Button clickButtonSingle;
private TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_click);
//寻找id放入全局变量里
clickButtonSingle = findViewById(R.id.ClickButtonSingle);
resultText = findViewById(R.id.ResultText);
//设置一个类MyOnClickListener实现接口View.OnClickListener
//再将这个类实例化new MyOnClickListener(),传入到setOnClickListener中
//当按钮点击时执行MyOnClickListener类里的onClick方法
clickButtonSingle.setOnClickListener(new MyOnClickListener(resultText));
}
static class MyOnClickListener implements View.OnClickListener {
//声明相关属性
private final TextView resultText;
//创建相关构造方法,将参数设为属性
public MyOnClickListener(TextView resultText) {
this.resultText = resultText;
}
@Override
public void onClick(View view) {
String desc = String.format("%s 您点击了按钮:%s", DataUtil.getNowtime(), ((Button) view).getText());
resultText.setText(desc);
}
}
}
<Button
android:id="@+id/ClickButtonSingle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单独点击监听器按钮"
android:textSize="20dp" />
<TextView
android:id="@+id/ResultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="此处查看结果" />
公共监听器
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener {
//直接在此方法中实现接口
//设置全局变量 ctrl+alt+f
private Button clickButtonSingle;
private TextView resultText;
private Button clickButtonPubilc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_click);
//寻找id
resultText = findViewById(R.id.ResultText);
clickButtonPubilc = findViewById(R.id.ClickButtonPubilc);
//直接在本类中实现OnClickListener接口,传入本类
clickButtonPubilc.setOnClickListener(this);
}
//ctrl+i快速实现接口方法
@Override
public void onClick(View view) {
//采用if判断来确定按下按钮和实现功能
if (view.getId() == R.id.ClickButtonPubilc) {
String desc = String.format("%s 您点击了按钮:%s", DataUtil.getNowtime(), ((Button) view).getText());
resultText.setText(desc);
}
//在此使用if判断多个按钮
}
}
推荐使用公共监听方法
长按点击监听
匿名内部类方式(之前的方法也适用)
//长按点击
Button longClickButton = findViewById(R.id.longClickButton);
longClickButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
resultText.setText("长按按钮事件");
return true;
}
}
转为lambda方式(jdk8以上)
//长按点击
//匿名内部类方式
Button longClickButton = findViewById(R.id.longClickButton);
longClickButton.setOnLongClickListener(view -> {
resultText.setText("长按按钮事件");
return true;
});
返回ture代表会消耗事件,即按钮的父容器不会触发长按点击事件,返回false不会消耗掉事件,按钮的父容器也会触发长按点击事件
禁用与恢复
使用setEnabled方法,ture为启用,false为禁用:
<?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"
tools:context=".ButtonEnableActivity"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/EnableButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:backgroundTint="#0000ff"
android:textSize="15dp"
android:text="启用测试按钮"/>
<Button
android:id="@+id/DisEnableButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:backgroundTint="#FF0000"
android:textSize="15dp"
android:text="关闭测试按钮"/>
</LinearLayout>
<Button
android:id="@+id/EnableTestButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#0000ff"
android:textColor="@color/white"
android:text="测试按钮"/>
<TextView
android:id="@+id/EnableTestText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:text="测试文本"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener {
private Button enableButton;
private Button disEnableButton;
private Button enableTestButton;
private TextView enableTestText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_enable);
enableButton = findViewById(R.id.EnableButton);
enableButton.setOnClickListener(this);
disEnableButton = findViewById(R.id.DisEnableButton);
disEnableButton.setOnClickListener(this);
enableTestButton = findViewById(R.id.EnableTestButton);
enableTestButton.setOnClickListener(this);
enableTestText = findViewById(R.id.EnableTestText);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.EnableButton:
enableTestButton.setEnabled(true);
enableTestButton.setBackgroundColor(Color.BLUE);
break;
case R.id.DisEnableButton:
enableTestButton.setEnabled(false);
enableTestButton.setBackgroundColor(Color.RED);
break;
case R.id.EnableTestButton:
String desc = String.format("%s 您点击了按钮:%s", DataUtil.getNowtime(),((Button)view).getText());
enableTestText.setText(desc);
}
}
}
图像显示
图像显示
<ImageView
android:id="@+id/iv_scale"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@drawable/ying_with_wife"/>
iv_scale = findViewById(R.id.iv_scale);
iv_scale.setImageResource(R.drawable.ying_with_wife);
图片的缩放
图像按钮
当一些文字符号无法打出又需要将其制作成时按钮时使用
<ImageButton
android:id="@+id/imageButton"
android:layout_width="200dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:layout_gravity="center"
android:src="@drawable/java"/>
如何使用点击事项待补充…(直接套用之前的匿名内部类和公共监视都会闪退,有时比如单纯设置文字时不会闪退)
同时使用图像和文字
同时展示文本与图像的可能途径包括:
- 利用LinearLayout对ImageView和TextView组合布局
- 通过按钮控件Button的drawable**属性设置文本周围的图标
- drawableTop:指定文字上方的图片。
- drawableBottom:指定文字下方的图片。
- drawableLeft:指定文字左边的图片。
- drawableRight:指定文字右边的图片。
- drawablePadding:指定图片与文字的间距。
op=“50dp”
android:layout_width=“match_parent”
android:layout_height=“250dp”
android:src=“@drawable/ying_with_wife”/>
```java
iv_scale = findViewById(R.id.iv_scale);
iv_scale.setImageResource(R.drawable.ying_with_wife);
图片的缩放
[外链图片转存中…(img-AgRxE56O-1652608917735)]
图像按钮
当一些文字符号无法打出又需要将其制作成时按钮时使用
<ImageButton
android:id="@+id/imageButton"
android:layout_width="200dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:layout_gravity="center"
android:src="@drawable/java"/>
如何使用点击事项待补充…(直接套用之前的匿名内部类和公共监视都会闪退,有时比如单纯设置文字时不会闪退)
同时使用图像和文字
同时展示文本与图像的可能途径包括:
- 利用LinearLayout对ImageView和TextView组合布局
- 通过按钮控件Button的drawable**属性设置文本周围的图标
- drawableTop:指定文字上方的图片。
- drawableBottom:指定文字下方的图片。
- drawableLeft:指定文字左边的图片。
- drawableRight:指定文字右边的图片。
- drawablePadding:指定图片与文字的间距。