资阳网站优化,风机网站怎么做,结构优化设计,法律电商如何做网站推广营销UI学习
UI分类
安卓的UI分为两大类#xff1a;一类叫做View视图#xff0c;一类叫做ViewGroup容器
View视图#xff1a;TextView,Button,ImageView都是常见的视图ViewGroup容器#xff1a;内部尅承载、放置、添加View视图的容器
布局方式
安卓布局主要有#xff1a;线…UI学习
UI分类
安卓的UI分为两大类一类叫做View视图一类叫做ViewGroup容器
View视图TextView,Button,ImageView都是常见的视图ViewGroup容器内部尅承载、放置、添加View视图的容器
布局方式
安卓布局主要有线性布局、相对布局、帧布局、约束布局
LinearLayout线性布局
LinearLayout线性布局横着或者竖着 按顺序排列 其属性主要有
orientation排列方向主要有vertical垂直排列、horizontal水平排列layout_width/height宽度主要有三种类型
填充父容器剩余空间match_parent根据子视图宽度自适应自己的宽高wrap_content自定义大小比如50dp
background背景颜色gravity重力决定子控件相对该父容器的位置。主要有三种 center居中显示 horizontal_center水平居中 vertical_center垂直居中layout_gravity决定该容器相对它的父容器的位置
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalandroid:background#FF2299android:gravitycenterButtonandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgroundTint#FF00FF/Buttonandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgroundTint#FFFF00/Buttonandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgroundTint#00FF00//LinearLayout RelativeLayout相对布局
在摆放子视图位置时按照指定的参考系摆放子视图的位置默认以屏幕左上角(0, 0)位置作为参考系摆放位置 主要有三大类
相对父元素相对兄弟元素相对兄弟元素对齐方式 ?xml version1.0 encodingutf-8?
RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentButtonandroid:idid/btn1android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:insetTop0dpandroid:insetBottom0dpandroid:text按钮1/Buttonandroid:idid/btn2android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:insetTop0dpandroid:insetBottom0dpandroid:text按钮2android:layout_toRightOfid/btn1android:layout_belowid/btn1/Buttonandroid:idid/btn3android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:insetTop0dpandroid:insetBottom0dpandroid:text按钮3android:layout_toRightOfid/btn2android:layout_belowid/btn2/Buttonandroid:idid/btn4android:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:insetTopdpandroid:insetBottom0dpandroid:text按钮4android:layout_toRightOfid/btn3android:layout_belowid/btn3/
/RelativeLayoutFrameLayout帧布局
组件的默认位置都是左上角组件之间可以重叠 设置方式与线性布局相似 具体UI
Button之MaterialButton
Material: 重要的 MaterialButton是Google推出的新控件比较好用
使用步骤 1.添加依赖 在app下的build.gradle文件下dependencies里添加依赖implementation com.google.android.material:material:1.10.0 然后执行Sync Now 2.修改app的theme主题 修改地方 将其parenter修改为Theme.MaterialComponents.Light.NoActionBar
一些常见属性 写的一些效果
MaterialButtonToggleGroup
类似于iOS的segment com.google.android.material.button.MaterialButtonToggleGroupandroid:idid/material_groupandroid:layout_widthmatch_parentandroid:layout_height60dpandroid:layout_marginLeft10dpandroid:layout_marginRight10dpandroid:gravitycenterapp:checkedButtonid/btn2com.google.android.material.button.MaterialButtonandroid:idid/btn1android:layout_width0dpandroid:layout_height50dpandroid:layout_weight1android:textTAB1app:cornerRadius15dpandroid:insetTop0dpandroid:insetBottom0dpandroid:gravitycenter/com.google.android.material.button.MaterialButtonandroid:idid/btn2android:layout_width0dpandroid:layout_height50dpandroid:layout_weight1android:textTAB2app:cornerRadius20dpandroid:insetTop0dpandroid:insetBottom0dpandroid:gravitycenter/com.google.android.material.button.MaterialButtonandroid:idid/btn3android:layout_width0dpandroid:layout_height50dpandroid:layout_weight1android:textTAB3app:cornerRadius20dpandroid:insetTop0dpandroid:insetBottom0dpandroid:gravitycenter//com.google.android.material.button.MaterialButtonToggleGroup监听的时候 //通过id找到对象val toggleGroup findViewByIdMaterialButtonToggleGroup(R.id.material_group)//对象添加监听//参数是一个接口前面需要加上object: 关键字toggleGroup.addOnButtonCheckedListener(object: MaterialButtonToggleGroup.OnButtonCheckedListener{//里面是实现接口的方法override fun onButtonChecked(group: MaterialButtonToggleGroup?,checkedId: Int,isChecked: Boolean) {Log.e(MaterialButton, ${checkedId}, ${isChecked.toString()});}})TextView
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenter_horizontalTextViewandroid:idid/textView2android:layout_width200dpandroid:layout_height160dpandroid:text111111111111111111111111111111android:textSize22dpandroid:textColor#ffffff//使用这个跑马灯才管用android:singleLinetrueandroid:backgroundcolor/purple_700android:layout_marginTop30dpandroid:ellipsizemarqueeandroid:marqueeRepeatLimit-1android:focusabletrueandroid:focusableInTouchModetrueandroid:drawableLeftandroid:drawable/btn_starandroid:drawableRightandroid:drawable/btn_starandroid:drawableTopandroid:drawable/btn_dialogandroid:drawableBottomandroid:drawable/btn_dropdown//LinearLayout在activity里面调用val tv findViewByIdTextView(R.id.textView2)
tv.requestFocus()ImageView 图片名不能以数字开头 center :保持原图的大小显示在ImageView的中心。当原图的长(宽)大于ImageView的长(宽)超过部分裁剪处理。centerCrop :以填满整个ImageView为目的将原图的中心对准ImageView的中心等比例放大原图直到填满ImageView为止指的是ImageView的宽和高都要填满原图超过ImageView的部分作裁剪处理。centerInside :以原图完全显示为目的将图片的内容完整居中显示通过按比例缩小原图的长(宽)等于或小于ImageView的长(宽)。如果原图的长宽本身就小于ImageView的长宽则原图不作任何处理居中显示在ImageView。fitCenter :把原图按比例扩大或缩小到ImageView的ImageView的高度居中显示fitEnd :把原图按比例扩大(缩小)到ImageView的高度显示在ImageView的下部分位置fitStart :把原图按比例扩大(缩小)到ImageView的高度显示在ImageView的上部分位置fitXY :把原图按照指定的大小在View中显示拉伸显示图片不保持原比例填满ImageView.matrix :不改变原图的大小从ImageView的左上角开始绘制原图原图超过ImageView的部 分作裁剪处理。 RecyclerView 类似tableViewcollectionView adapter数据源
来一个简单的List
private val data listOf(Apple, Banana, Orange, Watermelon, Pear, Grape, Pineapple, Strawberry, Cherry, Mango, Apple, Banana, Orange, Watermelon, Pear, Grape, Pineapple, Strawberry, Cherry, Mango)override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//注意android.R.layout.simple_list_item_1val adapter ArrayAdapterString(this, android.R.layout.simple_list_item_1, data)val recyclerView findViewByIdListView(R.id.listView)recyclerView.adapter adapter}在xml上写上ListViewListViewandroid:idid/listViewandroid:layout_widthmatch_parentandroid:layout_heightmatch_parent/自定义列表
使用recyclerView class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//找到recyclerViewval recyclerView findViewByIdRecyclerView(R.id.recyclerView)//管理者负责列表的布局recyclerView.layoutManager LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)recyclerView.adapter MyAdapter1()}//创建一个adapter负责数据的适配class MyAdapter1: RecyclerView.AdapterMyViewHolder() {override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {//找到itemval cellView LayoutInflater.from(parent.context).inflate(R.layout.textview_test, parent, false)return MyViewHolder(cellView)}//个数override fun getItemCount(): Int {return 20}override fun onBindViewHolder(holder: MyViewHolder, position: Int) {}}//负责列表的复用class MyViewHolder(view: View): RecyclerView.ViewHolder(view){}
} 跳转
显示跳转 val btn findViewByIdMaterialButton(R.id.jump_button)btn.setOnClickListener {val intent Intent(this, secondActivity::class.java)//传值intent.putExtra(key, 22222)startActivity(intent)}//接收值val dataString intent.getStringExtra(key)例子 作业结果