ScrollView水平滑动条选中条目的居中显示
来源:互联网 发布:2425端口 编辑:程序博客网 时间:2024/06/10 13:52
创建选择器实现圆角矩形 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="5dip"/> <solid android:color="#33ff0000"/> </shape>创建选择器实现字体颜色改变 <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_selected="true" android:color="@android:color/white"></item> <item android:state_pressed="true" android:color="@android:color/white"></item> <item android:color="@android:color/darker_gray"></item> </selector>创建选择器实现背景颜色改变 <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_selected="true" android:drawable="@drawable/bgshape_shape"></item> <item android:state_pressed="true" android:drawable="@drawable/bgshape_shape"></item> </selector>布局文件引用 <TextView android:layout_width="80dip" android:layout_height="wrap_content" android:gravity="center" android:textColor="@drawable/textcolor_selector" android:background="@drawable/bgcolor_selector" android:padding="5dip" android:text="热门" />MainActivity后台代码实现 private int screenWitdth; private HorizontalScrollView hsv; private LinearLayout ll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得屏幕宽度 screenWitdth = getResources().getDisplayMetrics().widthPixels; //初始化视图 initView(); //子控件点击事件 initEvent(); } //初始化视图 private void initView(){ hsv = (HorizontalScrollView) findViewById(R.id.hsv); ll = (LinearLayout) findViewById(R.id.ll); //默认选中第一个子控件 hsv.getChildAt(0).setSelected(true); } //定义子控件点击事件 private void initEvent(){ //获得水平滑动控件中子控件——水平线性布局内共有多少子控件 int childrenCount=hsv.getChildCount(); //对子控件循环监听 for(int i=0; i<childrenCount; i++){ final int currentIndex=i; //获得当前子控件 View childView=hsv.getChildAt(i); //对当前子控件设置点击监听事件 childView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //设置当前子控件诶选中 selectChildView(currentIndex); } }); } } //对点击选中的子控件进行设置 private void selectChildView(int position){ int childrenCount=hsv.getChildCount(); for(int i=0; i<childrenCount; i++){ //获得当前子控件对象,并设置为选中状态 View child=hsv.getChildAt(i); child.setSelected(true); } //设置选中条目居中 View currentView=hsv.getChildAt(position); int left=currentView.getLeft(); //获取点击控件与父控件左侧的距离 int width=currentView.getMeasuredWidth(); //获得控件本身宽度 int toX=left+width/2-screenWitdth/2; //使条目移动到居中显示 hsv.smoothScrollTo(toX, 0); }
0 0
- ScrollView水平滑动条选中条目的居中显示
- recycleview实现title横向滑动,点击条目居中显示
- Android开发——垂直水平滑动条scrollView和HorizontalScrollView的嵌套使用
- CheckBox监听显示选中的条目数
- 条目有限的RecyclerView 与ScrollView滑动冲突解决(仅限条目有限)
- 真正的画廊,左右条目可点击居中, 左右条目可滑动
- 真正的画廊,左右条目可点击居中, 左右条目可滑动
- ScrollView自定义水平滑动事件
- ScrollView隐藏滑动条
- DIV水平居中显示
- 解决在scrollview上下滑动嵌套scrollview,viewpager水平滑动时的抖动问题
- 解决在scrollview上下滑动嵌套scrollview,viewpager水平滑动时的抖动问题
- 解决水平ListView在ScrollView中出现的滑动冲突
- react-native设置scrollView水平垂直居中
- 实现水平listview,而且解决水平listview在scrollview中出现的滑动冲突
- 实现水平ListView,并且解决水平ListView在ScrollView中Listview中出现的滑动冲突
- 通过ImageView的点击监听事件来滑动ScrollView的滚动条,进而改变屏幕显示内容
- 导航条水平居中--未知宽度ul水平居中
- Qt connect
- Android中妙用Selector实现颜色图片切换效果
- JS 操作 window.location
- 【CNTK】CNTK学习笔记之图像识别-树叶识别Train and Test
- C++中explicit构造函数
- ScrollView水平滑动条选中条目的居中显示
- JavaScript强化教程——JavaScript 总结
- Android设置drawableTop图片大小
- mysql二进制安装
- minicom在centos和windows下使用
- 解决IDAE无法新建java文件
- HTTP基本认证(Basic Authentication)的JAVA示例
- BitmapFactory的使用获取Bitmap
- 写mysql存储过程应注意的几点