布局基础<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 ...
随机推荐
- 网络OSI七层模型及各层作用 与 TCP/IP
背景 虽然说以前学习计算机网络的时候,学过了,但为了更好地学习一些物联网协议(MQTT.CoAP.LWM2M.OPC),需要重新复习一下. OSI七层模型 七层模型,亦称OSI(Open System ...
- 如果shell到win上出现乱码怎么办
如果shell到win上出现这样的乱码怎么办? 如果出现了乱码,不慌一条命令搞定它!!! chcp 65001
- Codestorm:Game with a Boomerang
题目连接:https://www.hackerrank.com/contests/codestorm/challenges/game-with-a-boomerang 上一篇博客不知怎么复制过来题目, ...
- Day6-T1
原题目 Describe:模拟大水题 code: #include<bits/stdc++.h> #define INF 214748364 #define eps 1e-9 #defin ...
- R 《回归分析与线性统计模型》page93.6
rm(list = ls()) #数据处理 library(openxlsx) library(car) library(lmtest) data = read.xlsx("xiti4.xl ...
- lucene实践 - 索引维护、多域查询、高亮显示
之前的博客搜索栏用的是 sql 模糊查询进行查找,最近学完lucene,要学以致用啊,就把sql搜索给替换下来吧 中间遇到一些问题,也是学过程中没有提到的,所以说,还是实践出真知啊. lucene分开 ...
- 07.swoole学习笔记--tcp客户端
<?php //创建tcp客户端 $client=new swoole_client(SWOOLE_SOCK_TCP); //连接服务器 $client->connect(,) or di ...
- 七、Vue组件库:Element、Swiper(轮播专用组件)
一.vue的Element组件库 官网:https://element.eleme.cn/#/zh-CN 1.1安装 推荐安装方法: 首先要进入项目目录 cnpm i element-ui -S 或 ...
- c#使用Socket实现局域网内通信
服务器端代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...
- 09.swoole学习笔记--进程事件
<?php //进程数组 $workers=[]; //创建进程的数据量 $worker_num=; //创建启动进程 ;$i<$worker_num;$i++){ //创建单独新进程 $ ...