布局基础<kotlin>2,自定义控件(整理自网络)
引导页
ViewPager
代码清单
activity_main.xml
<?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="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg"
android:scaleType="centerCrop"/> <androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:id="@+id/myViewPage"/>
<LinearLayout
android:id="@+id/dot_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/myViewPage"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="..."
android:textSize="30sp"
android:textColor="#ccc"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/skipBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="跳过"
android:textColor="#fff"/>
<TextView
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:text="下一页"
android:textColor="#fff"/>
</RelativeLayout> </RelativeLayout>
page.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/pageIcon"
android:layout_width="match_parent"
android:layout_height="280dp"
android:src="@drawable/ic_face1"
android:layout_marginTop="80dp"/>
<TextView
android:id="@+id/pageTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/page_title1"
android:textSize="30dp"
android:gravity="center"
android:layout_marginTop="30dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/page_desc"
android:layout_margin="30dp"
/>
</LinearLayout>
ViewPagerAdapter.kt
package com.vocus.sepcialeffects import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter class ViewPagerAdapter:PagerAdapter() { override fun isViewFromObject(p0: View, p1: Any): Boolean {
return p0==p1
} override fun getCount(): Int {
return 3
} private val icons= intArrayOf(R.drawable.ic_face1,R.drawable.ic_face2,R.drawable.ic_face3)
private val titles= intArrayOf(R.string.page_title1,R.string.page_title2,R.string.page_title3) override fun instantiateItem(container: ViewGroup, position: Int): Any {
val page=LayoutInflater.from(container.context).inflate(R.layout.page,container,false)
val pageIcon=page.findViewById<ImageView>(R.id.pageIcon)
val pageTitle=page.findViewById<TextView>(R.id.pageTitle) pageIcon.setImageResource(icons[position])
pageTitle.text=container.resources.getText(titles[position]) container.addView(page)
return page
} override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
container.removeView(`object` as View)
}
}
MainActivity.kt
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) val adpter=ViewPagerAdapter()
myViewPage.adapter=adpter skipBtn.setOnClickListener{
finish()
}
nextBtn.setOnClickListener{
myViewPage.currentItem+=1
}
addDots() myViewPage.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
override fun onPageScrollStateChanged(state: Int) {
} override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) { } override fun onPageSelected(position: Int) {
addDots(position)
} }) } fun addDots(posi:Int=0){
val dots=arrayOf(TextView(this),TextView(this),TextView(this))
dot_container.removeAllViews()
dots.forEach {
it.text="."
it.textSize=30f
it.paint.isFakeBoldText=true
it.setTextColor(Color.GRAY)
dot_container.addView(it)
}
dots[posi].setTextColor(Color.WHITE)
}
}
效果
图片轮播(广告)
动态设置view的测试
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) var textView1= TextView(this)
textView1.textSize = 30f
textView1.text = "haha"
textView1.setTextColor(Color.BLUE)
textView1.id=R.id.textView1 var textView2 = TextView(this)
textView2.textSize = 30f
textView2.text = "xixi"
textView2.setTextColor(Color.BLUE) var params1=RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
params1.leftMargin=20
container.addView(textView1,params1) var params2=RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
params2.leftMargin=20
params2.addRule(RelativeLayout.RIGHT_OF,R.id.textView1)
container.addView(textView2,params2) }
其实跟制作引导页的方法差不多
MainActivity.kt
class MainActivity : AppCompatActivity() {
var imageViews = ArrayList<ImageView>()
var titles = listOf<String>("图片1", "图片2", "图片3", "图片4", "图片5")
var imagesIds = listOf<Int>(
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5
) override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var textView = findViewById<TextView>(R.id.myTitle) //需要初始化一下
textView.text = titles[0]
textView.setTextColor(Color.WHITE) var prePosition = 0 for (i in 0 until imagesIds.size) {
var imageView = ImageView(this) imageView.setBackgroundResource(imagesIds[i])
imageViews.add(imageView) var point = ImageView(this)
//point_selector.xml要自己创建
point.setBackgroundResource(R.drawable.point_selector)
if (i == 0) point.isEnabled = true else point.isEnabled = false
var params = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
params.leftMargin = 5
pointGroup.addView(point, params) } myViewPager.adapter = myPagerView(imageViews) myViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
} override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
} override fun onPageSelected(position: Int) {
textView.text = titles[position] pointGroup.getChildAt(prePosition).isEnabled = false
pointGroup.getChildAt(position).isEnabled = true
prePosition = position
} })
} class myPagerView() : PagerAdapter() {
private val imageViews = ArrayList<ImageView>() constructor(imageViews: ArrayList<ImageView>) : this() {
this.imageViews.addAll(imageViews)
} override fun isViewFromObject(view: View, `object`: Any): Boolean {
return view == `object`
} override fun getCount(): Int { return 5
} override fun instantiateItem(container: ViewGroup, position: Int): Any {
var view = imageViews.get(position)
container.addView(view)
return view
} override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
//super.destroyItem(container, position, `object`)
container.removeView(`object` as View)
}
} }
point_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/point_normal" android:state_enabled="false"></item> <item android:drawable="@drawable/point_press" android:state_enabled="true"></item>
</selector>
point_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<size android:width="5dp" android:height="5dp"/>
<solid android:color="#44000000"/>
</shape>
不过还要加上左右无限滑动和自动滑动。。
加上无限滑动只要把
override fun getCount(): Int {}中 返回的页面数改成Int.MAX_VALUE
在根据position获得图片列表资源的时候对position%viewImages.size取模
最后把当前页面位置设置为总页数的中间myViewPager.currentItem= Int.MAX_VALUE/2-Int.MAX_VALUE/2%imageViews.size
加上自动滑动用Handler
val handler:Handler=object:Handler(){
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
var item=myViewPager.currentItem+1
myViewPager.setCurrentItem(item) sendEmptyMessageDelayed(0,4000)
}
}
handler.sendEmptyMessageDelayed(0,4000)
布局基础<kotlin>2,自定义控件(整理自网络)的更多相关文章
- 布局基础<kotlin>(整理自网络)
全屏 主界面 底部导航,bottombar 添加依赖 implementation 'com.roughike:bottom-bar:2.3.1' 主界面布局 <com.roughike.bot ...
- 微信小程序学习笔记(7)--------布局基础
ui布局基础 一.flex布局 1.flex的容器和元素 2.flex容器属性详解 1>flex-direction不仅设置元素的排列方向,还设置主轴和交叉轴如下图主轴是由上到下 2&g ...
- 学习笔记 第十一章 CSS3布局基础
第11章 CSS3布局基础 [学习重点] 了解CSS2盒模型. 设计边框样式. 设计边界样式. 设计补白样式. 了解CSS3盒模型. 11.1 CSS盒模型基础 页面中所有元素基本显示形态为方形 ...
- mysql基础知识语法汇总整理(二)
mysql基础知识语法汇总整理(一) insert /*insert*/ insert into 表名(字段列表) values(值列表); --蠕虫复制 (优点:快速复制数据,测试服务器压力) in ...
- mysql基础知识语法汇总整理(一)
mysql基础知识语法汇总整理(二) 连接数据库操作 /*连接mysql*/ mysql -h 地址 -P 端口 -u 用户名 -p 密码 例如: mysql -u root -p **** /* ...
- Java面试之Java基础问题答案口述整理
Java面试之基础问题答案口述整理 面向对象的理解 面向对象思想就是在计算机程序设计过程中,把具体事物的属性特性和行为特征抽象出来,描述成计算机事件的设计思想.它区别于面向过程的思想,强调的是通过调用 ...
- css布局基础总结
前端css布局知识繁杂,实现方式多种多样.想写出高效.合理的布局,必须以深厚的css基础为前提.为了方便记忆和复习,将css布局要点记录如下.内容较多,应用方面说的不太详细,但都是很实用的点. 所谓布 ...
- 【理论面试篇】收集整理来自网络上的一些常见的 经典前端、H5面试题 Web前端开发面试题
##2017.10.30收集 面试技巧 5.1 面试形式 1) 一般而言,小公司做笔试题:大公司面谈项目经验:做地图的一定考算法 2) 面试官喜欢什么样的人 ü 技术好. ...
- 【编码题篇】收集整理来自网络上的一些常见的 经典前端、H5面试题 Web前端开发面试题
编写一个方法 求一个字符串的字节长度假设:一个英文字符占用一个字节,一个中文字符占用两个字节 function GetBytes(str){ var len = str.length; var byt ...
随机推荐
- Beta阶段计划
Beta阶段计划 JuJu 冲刺时间:12月27日至1月5号(遇到节假日顺延) 人员: 陈灿: 项目经理 金华:负责算法优化与提升 婷婷:同上 恩升:绘图 胡凯:对比pytorch的basel ...
- 题解 zr1212 【20WC集训】货币
题目链接 我们给每个连通块图上一种颜色.不同的连通块涂不同的颜色. 首先,我们定义\(f_r\)表示:使\([l,r]\)包括\([1,r]\)里所有颜色的最大的\(l\). 然后我维护一个变量\(p ...
- day07-Python运维开发基础(深/浅拷贝、字典/集合/相关操作)
1. 深拷贝与浅拷贝 # ### 深拷贝 和 浅拷贝 """ a = 7 b = a a = 8 print(b) lst1 = [1,2,3] lst2 = lst1 ...
- Linux下如何查找sqlnet.ora 和listener.ora 和tnsnames.ora 配置文件的目录
1.首先切换到oracle 用户下 使用env 查看数据库配置文件信息 2.然后找到LD_LIBRARY_PATH=/home/opt/oracle/product/11.2.0.4/db_1 (配置 ...
- oracle进入CDB
第一步:使用sys登陆 CONN sys/change_on_install AS SYSDBA; 第二步:查看现在的容器名称 SHOW con_name; 第三步:改变容器为PDB ALTER SE ...
- HTTP实战
1.建立http服务,要求: (1)提供两个基于名称的虚拟主机: www1.stuX.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1/erro ...
- POJ 1472:Instant Complexity 模拟时间复杂度
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1908 Accepted: 658 ...
- Html5 自学笔记
1 html的全称 Hyper Text Markup Language 2 HTML的意义 使用标记标签( Markup Tag)来描述网页 3 HTML标签一定成对吗 是 4 <html ...
- CentOS 6.8 32位 安装mysql8
1.清理掉之前安装过的mysql rpm -qa | grep mysql mysql-libs-5.1.52-1.el6_0.1.x86_64 yum remove mysql-libs-5.1.5 ...
- R 读取excel的方法
1.加载 readxl 包,利用 reade_excel() 函数 install.packages("readxl") library(readxl) data = read_e ...