Android自定义View实现通讯录字母索引(仿微信通讯录)
2019-12-16 21:44:27 来源:易采站长站 作者:于海丽
这里需要用到 WindowManager 容器,然需要现实的 View 附在这上面的就行,当手指按下的时候,让 View 显示出来,松开不显示就行了
/*设置浮动选中的索引*/
/*获取windowManager*/
mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
/*overly 视图,通过LayoutInflater 获取*/
mFloatView = LayoutInflater.from(getContext()).inflate(R.layout.overlay_indexview,null) ;
/*开始让其不可见*/
mFloatView.setVisibility(INVISIBLE);
/*转换 高度 和宽度为Sp*/
mOverlyWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,70,getResources().getDisplayMetrics()) ;
mOverlyHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,70,getResources().getDisplayMetrics()) ;
post(new Runnable() {
@Override
public void run() {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(mOverlyWidth,mOverlyHeight,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT) ;
mWindowManager.addView(mFloatView,layoutParams);
}
}) ;
同样的道理,如果需要改变显示的内容,就需要在调用回调的位置,为 View 中的 TextView 设置当前的索引内容。
好了此 View 的代码就这么多,
接下来就把引用他的 Xml 和浮动 View 的 Xml 也贴出来,
引用的布局文件
<moon.wechat.view.IndexView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"/>
浮动 View 的布局文件
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://www.easck.com/apk/res/android"
android:id="@+id/overly_text"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="A"
android:gravity="center"
android:background="@drawable/bg_overly_text"
android:textSize="40sp"
android:textColor="#ffffffff"
android:layout_gravity="center">
</TextView>
浮动 View 的背景
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://www.easck.com/apk/res/android">
<item>
<shape>
<solid android:color="#88000000"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持ASPKU!
注:相关教程知识阅读请移步到Android开发频道。
暂时禁止评论













闽公网安备 35020302000061号