布局基础<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)
浙公网安备 33010602011771号