activity是应用程序的组件
xml:描绘应用界面
java:编写程序逻辑
1.完整页面的创建过程:
-
在layout目录下创建xml文件
-
创建xml文件对应的java代码
-
在AndroidManifest中注册页面配置
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme._713" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
android:allowBackup:是否允许备份
android:icon:指定app在手机屏幕上显示的图标
android:label:指定app在手机屏幕上显示的名称
android:roundIcon:指定app的圆角图标
android:supportsRtl:是否支持阿拉伯语/波斯语这种从右往左的文字顺序 android:theme:指定app风格 android:dataExtractionRules:指定应用备份时的规则 android:fullBackupContent:定义了应用的全备份内容 tools:targetApi="31"
下面表示最开始执行的activity
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
1.创建文本
<?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" android:gravity="center"> <TextView android:id="@+id/t1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="xxxsssaaaqqq"/> </LinearLayout>
线性布局:<LinearLayout></LinearLayout>
match_parent:填充布局,即屏幕多大占多大;wrap_content:本身多大占多大
android:orientation:控制子视图排列方向
vertical:垂直方向;horizontal:水平方向
android:gravity:指定对其方式
top:将内容对齐到容器的顶部。
bottom:将内容对齐到容器的底部。
left:将内容对齐到容器的左侧。
right:将内容对齐到容器的右侧。
center_vertical:将内容在容器的垂直方向上居中对齐。
center_horizontal:将内容在容器的水平方向上居中对齐。
center:将内容在容器的水平和垂直方向上都居中对齐。
2.设置文本内容:
在java代码中使用setText修改文本内容
在xml文件中使用android:text=""定义文本内容
方法一:
在xml文件中直接使用
<TextView android:id="@+id/t1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="zxx6666"/>
在java文件中可以修改文本内容
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView t1=findViewById(R.id.t1); t1.setText("zcc zdd"); } }
setContentView:设置当前活动(Activity)的用户界面
findViewById:根据id查找布局文件的组件
setText:修改文本内容
方法二:
将字符串存在string中app/src/main/res/values/strings.xml
<resources> <string name="app_name">ActivityTest</string> <string name="text1">趙成成</string> <string name="text2">趙刀刀</string> </resources>
在layout下的xml文件中使用 android:text="@string/stringname"/ 来调用
<TextView android:id="@+id/t1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text1"/>
3.设置文本大小
在java中使用setTextSize方法指定大小
在xml文件中使用android:textSize指定文本大小
单位:px手机屏幕的最小显示单位与显示屏有关;dp与设备无关的显示单位,与屏幕尺寸有关;sp专门用来设置字体大小。
方法1.在java中修改文本大小
TextView text2 = findViewById(R.id.t2); text2.setTextSize(60);
方法2.在xml中修改文本大小
android:textSize="30sp"
4.修改字体颜色
在java代码中使用setTextColor方法设置文本颜色
1.使用自带的颜色t1.setTextColor(Color.GREEN);
2.自己设置颜色:
色值有八位十六进制数和六位十六进制数,
例如八位编码:FFEEDDCC中,
FF表示透明度,EE表示红色浓度,DD表示绿色浓度,CC表示蓝色浓度。
FF表示完全不透明,00表示完全透明,三色数值越大颜色越浓
六位十六进制即透明度为00
t1.setTextColor(Color.GREEN); t2.setTextColor(0xffff0000); t3.setTextColor(0x00ff00);
在xml文件中使用android:testColor指定文本颜色
在xml中六位十六禁止默认透明度为ff
android:textColor="#ff00ff"
在values/colors中定义好颜色然后在xml文件引用颜色
<color name="green">#FF00FF00</color>
android:textColor="@color/green"
t3.setTextColor(R.color.green)
设置背景颜色
1.在xml文件中使用android:background="@color/green"
2.在java文件中使用t4.setBackgroundColor(0xff0000ff)或者t4.setBackgroundResource(R.color.green)
5.视图显示
1设置视图宽高
1.在xml文件中
视图宽度通过属性:android:layout_width表示
视图高度通过属性:android:layout_height表示
取值有三种:
-
match_parent:表示与上级视图一致
-
wrap_content:表示与内容自适应
-
以dp为单位的具体尺寸
2.在java文件中
步骤
-
调用控件对象的getLayoutParams方法,获取该控件的布局参数
-
修改布局参数
-
调用setLayoutParams方法,填入修改后的布局参数生效
TextView t1=findViewById(R.id.t1); ViewGroup.LayoutParams params = t1.getLayoutParams(); params.width= Utils.dip2px(this, 200); t1.setLayoutParams(params);
注意:修改布局参数中的宽度数值默认单位px,需要把dp转成px
public class Utils { public static int dip2px(Context context, float dpValue){ float scale=context.getResources().getDisplayMetrics().density; return (int)(dpValue*scale+0.5f); } }
2.设置视图间距
方式一:设置layout_margin属性,指当前视图与周围平级视图之间的距离
layout_marginTop,layout_marginRight,layout_marginLeft,layout_marginBottom
方法二:设置padding属性,指定当前视图与内部下级视图之间的距离
paddingTop,paddingLeft,paddingRight,paddingBottom
3.设置视图的对齐方式
方式一:layout_gravity,指定当前视图相对于上级视图的对齐方式
方式二:gravity,指定下级视图相对于当前视图的对齐方式
取值:left,right,top,bottom,可以使用"|"连接取值
6.布局
6.1线性布局LinearLayout
两种orientation值
horizontal,内部视图在水平方向从左往右排列
vertical,内部视图在垂直方向从上往下排列
layout_weight:权重
当layout_width="0dp",layout_wieght表示水平方向的宽度比例
当layout_height="0dp",layout_weight表示垂直方向的高度比例
6.2相对布局RelativeLayout
相对布局的下级视图位置由其他视图决定,用于确定下级视图位置的参照物有两种:与该视图平级的视图,该视图的上级视图。
如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角
android:layout_toLeftOf="@id/xxx"
android:layout_centerInParent="true"
属性取值 | 说明 |
---|---|
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 | 当前视图与上级视图的底部对齐 |
6.3网格布局GridLayout
支持多行多列的表格排列,网格布局默认从左往右,从上往下。
columnCount:指定网格列数
rowCount:指定网格行数
6.4约束布局androidx.constraintlayout.widget.ConstraintLayout
ConstraintLayout 允许你通过定义元素之间的相对位置(如相对于父布局、兄弟元素或它们自身的某一边)来布局界面元素。这种布局方式比传统的布局(如 LinearLayout 和 RelativeLayout)更灵活、更强大。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toStartOf="@+id/button2" android:layout_marginEnd="16dp" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" app:layout_constraintStart_toEndOf="@+id/button1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="16dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
7.滚动视图
7.1ScrollView:
垂直方向的滚动视图;垂直方向滚动时,layout_width属性值为match_parent,layout_height属性值为wrap_content.
7.2HorizontalScrollView:
水平方向的滚动视图;水平方向滚动时,layout_width设置为wrap_content,layout_height设置为match_parent.
8.控件Button
Button由TextView派生而来,区别:
-
Button拥有默认背景,TextView默认无背景;
-
Button默认内部文本居中对齐,TextView默认左对齐;
-
Button默认英文大写,TextView保持原始英文大小写;
注意:button当首字母大写是才默认英文大写
属性:
textAllCaps:指定是否将英文字母转换为大写,true大写
onClick:用来接管用户点击的动作
8.1onClick:
//获取时间工具,Date需要通过函数转为字符串 public class DateUtil { public static String getCurrentTime() { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); return sdf.format(new Date()); } }
8.2点击监听器setOnClickListener
点击时间三种方法,下面包含两种
按住少于500毫秒会触发点击事件
public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener{ private TextView t1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button_click); Button b1=findViewById(R.id.b1); t1= findViewById(R.id.t1); b1.setOnClickListener(new MyClickListener(t1)); Button b2=findViewById(R.id.b2); b2.setOnClickListener(this); } //方法二 @Override public void onClick(View view) { if(view.getId()==R.id.b2){ String str=String.format("%s点击事件%s", DateUtil.getCurrentTime(),((Button)view).getText()); t1.setText(str); } } //方法一 static class MyClickListener implements View.OnClickListener{ private final TextView t1; public MyClickListener(TextView t1) { this.t1 = t1; } @Override public void onClick(View view) { String str=String.format("%s点击事件%s", DateUtil.getCurrentTime(),((Button)view).getText()); t1.setText(str); } } }
8.3长按监听器setOnLongClickListener
按住大于500毫秒会触发长按监听事件
第三种点击事件方法
//方法三 b1.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { return false; } });
可以替换lambda:
b1.setOnLongClickListener(view -> false);
false表示传给父容器,true表示自己消化
b1.setOnLongClickListener(view -> { String str=String.format("%s点击事件%s", DateUtil.getCurrentTime(),((Button)view).getText()); t1.setText(str); return true; });
9.禁用与恢复按钮
是否允许点击有enabled属性控制
通过setEnabled来控制如:b3.setEnabled(true);
public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener { private TextView t1; private Button b3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button_enable); Button b1=findViewById(R.id.b1); Button b2=findViewById(R.id.b2); b3 = findViewById(R.id.b3); t1 = findViewById(R.id.t1); // b1.setOnClickListener(view -> { // b3.setEnabled(true); // }); // b2.setOnClickListener(view -> { // b3.setEnabled(false); // }); // b3.setOnClickListener(view -> { // String str=String.format("%s点击事件%s", DateUtil.getCurrentTime(),((Button)view).getText()); // t1.setText(str); // }); //alt+enter b1.setOnClickListener(this); b2.setOnClickListener(this); b3.setOnClickListener(this); } @Override public void onClick(View view) { if (view.getId() == R.id.b1) { b3.setEnabled(true); }else if (view.getId() == R.id.b2) { b3.setEnabled(false); }else if (view.getId() == R.id.b3) { String str=String.format("%s点击事件%s", DateUtil.getCurrentTime(),((Button)view).getText()); t1.setText(str); } } }
10.图像显示
10.1图像视图
图片通常位于res/drawable***目录,设置图片视图显示的两种方式:
在xml中通过属性android:src设置图片资源,属性格式如"@drawable/不含扩展名的图片名称"
在java文件中调用setImageResource方法设置图片资源,方法参数如"R.drawable.不含扩展名的图片名称"
10.2图像缩放类型
ImageView默认图片居中显示,若要修改图片显示方式,通过修改scaleType属性设定
在xml中android:scaleType="centerInside"
在java文件中i1.setScaleType(ImageView.ScaleType.CENTER);
xml中的缩放类型 | ScaleType类中的缩放类型 | 说明 |
---|---|---|
fitXY | FIT_XY | 拉伸图片使其刚好填满整个屏幕 |
fitStart | FIT_START | 保持宽高比例,拉伸图片位于视图上方或者左侧 |
fitCenter | FT_CENTER | 保持宽高比例,拉伸图片使其位于视图中间 |
fitEnd | FIT_END | 保持宽高比例,拉伸图片使其位于视图下方或右侧 |
center | CENTER | 保持图片原尺寸,并使其位于视图中间 |
centerCrop | CENTER_CROP | 拉伸图片使其充满视图,并位于视图中间 |
centerInside | CENTER_INSIDE | 保持宽高比例,缩小图片使其位于视图中间 |
10.3图像按钮
button和imagebutton的区别:
-
button即可显示文本也可显示图片,imagebutton只能显示图片
-
imagebutton的图像可按比例缩放,button通过背景设置的图像会拉伸变形
-
button只能靠背景显示一张图片,imagebutton可分别在前景和背景显示图片,从而实现两张图片叠加的效果
imagebutton和imageview之间的区别:
-
imagebutton有默认的按钮背景,imageview无默认背景
-
imagebutton默认缩放类型为center,Imageview默认缩放类型为fitcenter
10.4同时展示文本与图像
方法一:利用linaerlayout对imageview和textview组合布局
方法二:通过按钮控件button的drawable**属性设置文本周围的图标
drawableTop:指定文字上方的图片
drawableBottom:指定文字下方的图片
drawableRight:指定文字右边的图片
drawableLeft:指定文字左边的图片
drawablePadding:指定图片与文字的间距
补充:
1.尺寸
名称 | 解释 |
px(像素) | 作为图像构成的基本单元,单个像素的大小不固定,跟随屏幕大小和像素数量的关系变化,一个像素点为1px |
resolution(分辨率) | 指屏幕的垂直和水平方向的像素数量,如199*111,即垂直方向199个像素,水平方向111个像素 |
Dpi(像素密度) | 指每英寸有多少像素点 |
density(密度) | 指每平方英寸有多少个像素点 |
Dip/dp(设备独立像素) | 长度单位,不同设备不同效果 |
计算规则:以4.95英寸1920*1080为例
Dpi:
1.根据勾股定理计算斜边 :1920^2+1080^2=2202^2
2.Dpi=2202/4.95=445
即这台设备dpi为445
Density:445*445=198025
dip:
px=dip*dpi/160
2.ctrl+alt+f变成全局变量
3.
b1.setOnClickListener(view -> { b3.setEnabled(true); }); b2.setOnClickListener(view -> { b3.setEnabled(false); }); b3.setOnClickListener(view -> { String str=String.format("%s点击事件%s", DateUtil.getCurrentTime(),((Button)view).getText()); t1.setText(str); });
b1.setOnLongClickListener(view -> { String str=String.format("%s点击事件%s", DateUtil.getCurrentTime(),((Button)view).getText()); t1.setText(str); return true; });
resulttext=String.valueOf(Math.pow(Double.parseDouble(firsttext),2));
标签:layout,day2,视图,t1,学习,对齐,android,view From: https://blog.csdn.net/qq_57170705/article/details/140446986