金溪网站建设制作,怎么开发游戏软件赚钱,北京市e窗口工商登记,vps除了做网站还能做什么目录 一、文本显示1.1 设置文本内容1.2 设置文本大小1.3 设置文本颜色 二、视图基础2.1 设置视图宽高2.2 设置视图间距2.3 设置视图对齐方式 三、常用布局3.1 线性布局LinearLayout3.2 相对布局RelativeLayout3.3 网格布局GridLayout3.4 滚动视图ScrollView 四、按钮触控4.1 按… 目录 一、文本显示1.1 设置文本内容1.2 设置文本大小1.3 设置文本颜色 二、视图基础2.1 设置视图宽高2.2 设置视图间距2.3 设置视图对齐方式 三、常用布局3.1 线性布局LinearLayout3.2 相对布局RelativeLayout3.3 网格布局GridLayout3.4 滚动视图ScrollView 四、按钮触控4.1 按钮控件4.2 点击和长按事件4.3 禁用与恢复按钮 五、图像显示5.1 图像视图ImageView5.2 图像按钮ImageButton5.3 同时展示文本与图像 一、文本显示
1.1 设置文本内容 android:text 属性 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentTextViewandroid:idid/tv_helloandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/hello/
/LinearLayout1.2 设置文本大小
字体大小用sp单位 android:textSize 属性 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalTextViewandroid:idid/tv_dpandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/helloandroid:textSize30sp/
/LinearLayout1.3 设置文本颜色 android:textColor 属性 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalTextViewandroid:idid/tv_code_systemandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text代码设置系统自动的颜色代码android:textSize17sp/TextViewandroid:idid/tv_code_eightandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text代码设置8位颜色android:textSize17sp/TextViewandroid:idid/tv_code_sixandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text代码设置6位颜色android:textSize17sp/TextViewandroid:idid/tv_xmlandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textxml设置6位颜色android:textSize17spandroid:textColor#ff00ff/TextViewandroid:idid/tv_valuesandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textxml设置6位颜色android:textSize17spandroid:textColorcolor/teal_200/TextViewandroid:idid/tv_code_backgroundandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text背景设置绿色android:textSize17sp/!-- android:backgroundcolor/teal_200 --
/LinearLayout二、视图基础
2.1 设置视图宽高
视图宽高和间距用dp单位 android:layout_width 设置宽度 android:layout_height 设置高度 wrap_content 由内容撑开match_parent 匹配父容器 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textColorcolor/teal_200android:layout_marginTop5dpandroid:backgroundcolor/blackandroid:textSize17sp//LinearLayout2.2 设置视图间距
间距用dp单位 这里和前端的css属性非常类似比如左边距margin-lfet在安卓中就是layout_marginLeft android:padding 设置内边距 android:layout_margin 设置外边距 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_height300dpandroid:orientationverticalandroid:background#00aaffandroid:padding30dp!--中间层布局颜色为黄色--LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:layout_margin20dpandroid:background#ffff99android:padding60dp!--内层视图颜色为红色--Viewandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:background#00ff00 //LinearLayout/LinearLayout2.3 设置视图对齐方式 android:layout_gravity 设置父容器的对齐方式 android:gravity 设置子组件在父容器的对齐方式 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_height300dpandroid:layout_widthmatch_parentandroid:background#ffff99android:orientationhorizontal!-- 第一个子布局背景为红色它在上级视图中朝下对齐它的下级视图则靠左对齐 --LinearLayoutandroid:layout_width0dpandroid:layout_height200dpandroid:layout_weight1android:layout_margin10dpandroid:padding10dpandroid:background#ff0000android:layout_gravitybottomandroid:gravitycenter!--内部视图的宽度和高度都是100dp且背景色为青色--Viewandroid:layout_width100dpandroid:layout_height100dpandroid:backgroundcolor/teal_200//LinearLayout!--第二个子布局背景为红色它在上级视图中朝上对齐它的下级视图则靠右对齐--LinearLayoutandroid:layout_width0dpandroid:layout_height200dpandroid:layout_weight1android:layout_margin10dpandroid:padding10dpandroid:background#ff0000android:gravityright!--内部视图的宽度和高度都是100dp且背景色为青色--Viewandroid:layout_width100dpandroid:layout_height100dpandroid:backgroundcolor/teal_200//LinearLayout/LinearLayout 三、常用布局
3.1 线性布局LinearLayout
LinearLayout 为线性布局它可以通过android:orientation 来设置页面的排列方向vertical是垂直方向horizontal是水平方向排列 代码示例
!--水平排列--LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text横排第一个android:textSize17spandroid:textColor#000000/TextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text横排第二个android:layout_marginLeft10dpandroid:textSize17spandroid:textColor#000000//LinearLayout3.2 相对布局RelativeLayout
相对布局可以相对某一个组件设置对齐方式比如要让A组件在B组件的下面就可以使用android:layout_belowid/B 常用属性如下
android:layout_centerInParenttrue 在父容器中间对齐android:layout_centerHorizontaltrue 在父容器水平居中android:layout_centerVerticaltrue 在父容器垂直居中android:layout_alignParentLefttrue 在父容器左边对齐android:layout_alignParentRighttrue 在父容器右边对齐android:layout_alignParentToptrue 在父容器顶部对齐android:layout_alignParentBottomtrue 在父容器底部对齐android:layout_toLeftOfid/tv_center 在tv_center组件的左边android:layout_toRightOfid/tv_center 在tv_center组件的右边android:layout_aboveid/tv_center 在tv_center组件的上边android:layout_belowid/tv_center 在tv_center组件的下方android:layout_alignTopid/tv_center 与tv_center组件顶部对齐android:layout_alignBottomid/tv_center 与tv_center组件底部对齐android:layout_alignLeftid/tv_center 与tv_center组件左边对齐android:layout_alignRightid/tv_center 与tv_center组件右边对齐
3.3 网格布局GridLayout
网格布局就是类似表格一样的布局用起来还是很方便的 常用属性
属性作用android:columnCount设置列数android:rowCount设置行数android:layout_columnWeight设置列宽的权重android:layout_rowWeight纵向乘剩余空间分配方式android:layout_rowSpan横向跨几行android:layout_columnSpan横向跨几列
代码示例
?xml version1.0 encodingutf-8?
GridLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:columnCount2android:rowCount2TextViewandroid:layout_height60dpandroid:layout_width0dpandroid:layout_columnWeight1android:text浅红色android:background#ffccccandroid:textColor#000000android:textSize17spandroid:gravitycenter/TextViewandroid:layout_height60dpandroid:layout_width0dpandroid:layout_columnWeight1android:text橙色android:background#ffaa00android:textColor#000000android:textSize17spandroid:gravitycenter/TextViewandroid:layout_height60dpandroid:layout_width0dpandroid:layout_columnWeight1android:text绿色android:background#00ff00android:textColor#000000android:textSize17spandroid:gravitycenter/TextViewandroid:layout_height60dpandroid:layout_width0dpandroid:layout_columnWeight1android:text紫色android:background#660066android:textColor#000000android:textSize17spandroid:gravitycenter/
/GridLayout3.4 滚动视图ScrollView
滚动视图分为垂直滚动和水平滚动 1.水平滚动HorizontalScrollView
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationvertical!--水平滚动--HorizontalScrollViewandroid:layout_widthwrap_contentandroid:layout_height200dp!-- 水平方向的线性布局两个于视图的颜色分别为青色和黄色--LinearLayoutandroid:layout_widthwrap_contentandroid:layout_heightmatch_parentandroid:orientationhorizontalViewandroid:layout_width300dpandroid:layout_heightmatch_parentandroid:background#aaffff /Viewandroid:layout_width300dpandroid:layout_heightmatch_parentandroid:background#aaff00//LinearLayout/HorizontalScrollView/LinearLayout2. 垂直滚动ScrollView
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationvertical!--垂直滚动--ScrollViewandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentLinearLayoutandroid:layout_widthwrap_contentandroid:layout_heightmatch_parentandroid:orientationverticalViewandroid:layout_widthmatch_parentandroid:layout_height400dpandroid:background#00ff00 /Viewandroid:layout_widthmatch_parentandroid:layout_height400dpandroid:background#ffffaa//LinearLayout/ScrollView
/LinearLayout四、按钮触控
可以通过findViewById找到在xml中定义的组件只要在xml中定义组件时指定id即可
4.1 按钮控件
按钮控件用Button标签,按钮控件自带样式如果想要自定义样式要先修改res-values-themes.xml中的parent属性值为Theme.MaterialComponents.DayNight.DarkActionBar.Bridge 代码示例 Buttonandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:textHello worldandroid:textColorcolor/blackandroid:textSize17sp/4.2 点击和长按事件
1.点击事件 定义两个按钮演示不同的绑定事件的方法 Buttonandroid:idid/btn_click_singleandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:text指定点击事件监听android:textColor#000000android:textSize17sp/Buttonandroid:idid/btn_click_publicandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:text指定公点击事件监听android:textColor#000000android:textSize17sp/在ButtonClickActivity中绑定监听事件。绑定监听事件有两种方式第一种让本类实现View.OnClickListener接口重写onClick方法第二种是自定义一个类实现View.OnClickListener接口重写onClick方法 public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener{private TextView tv_result;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_button_click);tv_result findViewById(R.id.tv_result);Button btn_click_single findViewById(R.id.btn_click_single);Button btn_click_public findViewById(R.id.btn_click_public);btn_click_single.setOnClickListener(new MyOnClickListener(tv_result));btn_click_public.setOnClickListener(this);}//第二种方式Overridepublic void onClick(View v) {if (v.getId() R.id.btn_click_public){String s String.format(%s 你点击了按钮 %s, DateUtil.getNowTime(), ((Button) v).getText());tv_result.setText(s);}}//第一种方式static class MyOnClickListener implements View.OnClickListener{private final TextView tv_result;public MyOnClickListener(TextView tv_result) {this.tv_result tv_result;}Overridepublic void onClick(View v) {String s String.format(%s 你点击了按钮 %s, DateUtil.getNowTime(), ((Button) v).getText());tv_result.setText(s);}}
}4.3 禁用与恢复按钮 按钮的禁用和启动主要通过enabled属性来控制false禁用true启用 可以通过xml配置也可通过java代码设置。 1.xml设置 Buttonandroid:idid/btn_testandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:text测试按钮android:enabledfalseandroid:textColor#888888android:textSize17sp/2.java代码设置
public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener{private Button btn_test;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_button_enable);btn_test findViewById(R.id.btn_test);//启用true|禁用falsebtn_test.setEnabled(true);}
}五、图像显示
标签ImageView 1.android:adjustViewBounds设置ImageView是否调整自己的边界来保持所显示图片的长宽比。 2.android:maxHeight设置ImageView的最大高度。 3.android:maxWidth设置ImageView的最大宽度。 5.android:src设置ImageView所显示的Drawable对象的ID。 6.android:scaleType 图像在ImageView中的显示效果,下面是一些常用属性
fitXY 横向、纵向独立缩放以适应该ImageView。fitStart:保持纵横比缩放图片并且将图片放在ImageView的左上角。fitCenter保持纵横比缩放图片缩放完成后将图片放在ImageView的中央。fitEnd保持纵横比缩放图片缩放完成后将图片放在ImageView的右下角。center把图片放在ImageView的中央但是不进行任何缩放。centerCrop保持纵横比缩放图片以使图片能完全覆盖ImageView。centerInside保持纵横比缩放图片以使得ImageView能完全显示该图片。
图片资源放在下图中注意不能用数字命名开头
5.1 图像视图ImageView
代码示例
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalImageViewandroid:idid/iv_scaleandroid:layout_widthmatch_parentandroid:layout_height220dpandroid:layout_marginTop5dpandroid:scaleTypecenterInsideandroid:srcdrawable/test/!--android:srcdrawable/ic_launcher_background--
/LinearLayout5.2 图像按钮ImageButton
标签是ImageButton它继承于Button类 代码示例
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalImageButtonandroid:layout_widthmatch_parentandroid:layout_height80dpandroid:scaleTypecenterCropandroid:srcdrawable/test /
/LinearLayout5.3 同时展示文本与图像
常用属性值
android:drawableBottom 底部添加图片android:drawableEnd 在末尾添加图片android:drawableLeft 在左边添加图片android:drawableRight 在右边添加图片android:drawabLeStart 在开始位置添加图片android:drawableTop 在顶部添加图片
给Button添加图片和文字 代码示例
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalButtonandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text图标在左android:drawableLeftdrawable/btnandroid:background#ffffffandroid:drawablePadding5dp/
/LinearLayout