自定义dialog布局
class ShareDialog(context: Context) : AlertDialog(context) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/*圆角布局*/
val layout = ConnerFrameLayout(context)
layout.setBackgroundColor(Color.WHITE)
layout.setViewOutLine(20,ViewHelper.SIDE_BOTTOM)
/*分享内容*/
val mRecyclerView = RecyclerView(context)
mRecyclerView.apply {
layoutManager = GridLayoutManager(context, 4)
adapter = ShareAdapter(context, queryShareItems(), "")
}
/*布局参数*/
val params = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT
)
//item margin
val margin = dp2px(0)
params.apply {
leftMargin = margin
topMargin = margin
rightMargin = margin
bottomMargin = margin
}
params.gravity = Gravity.CENTER
layout.addView(mRecyclerView, params)
//设置dialog布局
setContentView(layout)
//显示在底部
window!!.setGravity(Gravity.BOTTOM)
window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
window!!.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
}
private fun dp2px(dpValue: Int): Int {
val displayMetrics = context.resources.displayMetrics
return (displayMetrics.density*dpValue+0.5f).toInt()
}
/*布局Adapter*/
private fun queryShareItems(): List<ResolveInfo> {
val items = arrayListOf<ResolveInfo>()
val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.type = "text/plain"
val providers =
context.packageManager.queryIntentActivities(intent, 0)
providers.forEach {
val packageName = it.activityInfo.packageName
if(TextUtils.equals(packageName,"com.tencent.mm") ||
TextUtils.equals(packageName,"com.tencent.mobileqq")){
items.add(it)
}
}
return items
}
}
布局内容adapter
class ShareAdapter(val context:Context, val data: List<ResolveInfo>, private val shareContent:String) :
RecyclerView.Adapter<ShareAdapter.InnerViewHolder>() {
lateinit var mPackManager: PackageManager
class InnerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
lateinit var mShareText: TextView
lateinit var mShareIcon: ImageView
init {
mShareIcon = view.findViewById<ImageView>(R.id.share_icon)
mShareText = view.findViewById<TextView>(R.id.share_text)
}
}
init {
mPackManager = context.packageManager
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerViewHolder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.layout_share_item, parent, false)
return InnerViewHolder(view)
}
override fun getItemCount(): Int {
return data.size
}
override fun onBindViewHolder(holder: InnerViewHolder, position: Int) {
val resolveInfo = data[position]
val shareIcon = resolveInfo.loadIcon(mPackManager)
holder.mShareIcon.setImageDrawable(shareIcon)
val mShareLabel = resolveInfo.loadLabel(mPackManager)
holder.mShareText.text = mShareLabel
holder.itemView.setOnClickListener {
val pkg = resolveInfo.activityInfo.packageName
val cls = resolveInfo.activityInfo.name
val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.type = "text/plain"
intent.component = ComponentName(pkg, cls)
intent.putExtra(Intent.EXTRA_TEXT, shareContent)
context.startActivity(intent)
}
}
}
设置圆角
object ViewHelper {
/*圆角大小*/
var radius: Int = 0
/*圆角位置*/
var side: Int = 0
const val SIDE_ALL = 0
const val SIDE_LEFT = 1
const val SIDE_TOP = 2
const val SIDE_RIGHT = 3
const val SIDE_BOTTOM = 4
/*获取自定义属性*/
fun setOutline(view: View, attributeSet: AttributeSet?) {
val array =
view.context.obtainStyledAttributes(attributeSet, R.styleable.ViewOutLine)
radius = array.getDimensionPixelSize(R.styleable.ViewOutLine_clip_radius, 0)
side = array.getIndex(R.styleable.ViewOutLine_clip_side)
array.recycle()
setOutlineView(view, radius, side)
}
/*设置view轮廓*/
fun setOutlineView(view: View, radius: Int, side: Int) {
view.outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline?) {
val w = view.width
val h = view.height
/*判断view宽高*/
if(w <= 0 || h <=0){
return
}
var left = 0
var top = 0
var right = w
var bottom = h
/*判断圆角位置*/
if(side != SIDE_ALL){
when(side){
SIDE_LEFT -> right += radius
SIDE_TOP -> bottom += radius
SIDE_RIGHT -> left -= radius
SIDE_BOTTOM -> top -= radius
}
outline!!.setRoundRect(left,top,right,bottom,radius.toFloat())
return
}
if(radius <= 0){
outline!!.setRect(left,top,right,bottom)
}else{
outline!!.setRoundRect(left,top,right,bottom, radius.toFloat())
}
}
}
view.clipToOutline = radius>0
view.invalidate()
}
}
自定义圆角view
class ConnerFrameLayout(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
FrameLayout(context, attrs, defStyleAttr) {
constructor(context: Context, attrs: AttributeSet?):this(context,attrs,-1)
constructor(context: Context):this(context,null,-1)
init {
ViewHelper.setOutline(this,attrs)
}
fun setViewOutLine(radius: Int, side: Int){
ViewHelper.setOutlineView(this,radius,side)
}
}
使用
mShare.setOnClickListener {
ShareDialog(this).show()
}
自定义属性
<!--view outline-->
<declare-styleable name="ViewOutLine">
<attr name="clip_radius" format="integer"/>
<!--圆角方向-->
<attr name="clip_side" format="enum">
<enum name="all" value="0"/>
<enum name="left" value="1"/>
<enum name="top" value="2"/>
<enum name="right" value="3"/>
<enum name="bottom" value="4"/>
</attr>
</declare-styleable>
网友评论