自定义可适配宽度的listview,以便适配popupwindow
来源:互联网 发布:大作家写作软件 编辑:程序博客网 时间:2024/06/04 19:12
第一次写博客,写得不好之处请大家多多包含,欢迎拍砖。
最近公司项目有个这样的需求,直接上demo截图:
我想到的思路主要有两种:
第一种是在LinearLayout布局中动态添加TextView,设置LinearLayout的属性为水平居中,TextView的属性包裹内容;
第二种是自定义ListView,取item的最大宽度作为ListView的宽度。
感觉第二种的封装性比较好,耦合度比较低,所以就用第二种方法实现了:
自适应宽度的ListView的代码实现:
/** * Created by xiaoming on 2016/9/6. * 计算listview 每个item的宽度,取最长长度作为listview的宽度 */public class ListViewAdaptWidth extends ListView { public ListViewAdaptWidth(Context context) { super(context); } public ListViewAdaptWidth(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public ListViewAdaptWidth(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getMaxWidthOfChildren() + getPaddingLeft() + getPaddingRight();//计算listview的宽度 super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), heightMeasureSpec);//设置listview的宽高 } /** * 计算item的最大宽度 * * @return */ private int getMaxWidthOfChildren() { int maxWidth = 0; View view = null; int count = getAdapter().getCount(); for (int i = 0; i < count; i++) { view = getAdapter().getView(i, view, this); view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); if (view.getMeasuredWidth() > maxWidth) maxWidth = view.getMeasuredWidth(); } return maxWidth; }}item的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:textColor="@color/white" android:text="佛冈" /></LinearLayout>主要代码片段:
public void showPopupWindow(View view) { if (mPopupWindow == null) { View contentView = LayoutInflater.from(this).inflate(R.layout.popup_listviewadaptwidth, null); ListViewAdaptWidth listViewAdaptWidth = (ListViewAdaptWidth) contentView.findViewById(R.id.lv_adapt_width); listViewAdaptWidth.setAdapter(mPopLvAdapter); listViewAdaptWidth.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mPopupWindow != null) mPopupWindow.dismiss(); } }); mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); mPopupWindow.setBackgroundDrawable(new ColorDrawable()); mPopupWindow.setOutsideTouchable(true); } int[] position = new int[2]; view.getLocationOnScreen(position); int y = position[1] + view.getMeasuredHeight(); mPopupWindow.showAtLocation(view, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, y); }这是我的第一篇文章,欢迎吐槽、交流。
1 0
- 自定义可适配宽度的listview,以便适配popupwindow
- PopupWindow,ListView实现自定义Spinner
- Android 自定义PopupWindow 实现 ListView与PopupWindow结合下拉框
- android自定义tabwidget,popupwindow,spinner,dialog,listview
- Android PopupWindow加入自定义圆角listview
- 自适应宽度的ListView
- 自定义PopupWindow的实现
- 自定义布局的PopupWindow
- 自定义的PopupWindow
- 自定义PopupWindow的使用
- 解决PopupWindow中显示ListView时不能自适配窗口大小的问题
- 机型适配问题 三星note3上popupWindow里的listView无法获得点击事件
- Android listview自定义分割线宽度
- PopupWindow中显示ListView时自适配窗口大小
- PopupWindow中显示ListView时自适配窗口大小
- popupwindow自定义半透明的菜单
- 自定义PopupWindow的监听事件
- 一个简单的自定义popupwindow
- 聚类分析总结
- 重写listview/gridview 的onMeasure方法,解决与ScrollView的冲突
- java常用jar包(值得收藏)
- 算法题
- 深入理解Activity的生命周期
- 自定义可适配宽度的listview,以便适配popupwindow
- 【完整的App项目】颖火虫笔记v2
- 矩阵转置
- 简单工厂模式(StaticFactoryMethod)
- 内存泄漏
- VMware Workstation虚拟机显示屏幕太小问题解决方法
- TCP滑动窗口与回退N针协议
- linux下在terminal启动刚下载的应用方法
- |洛谷|NOIP2011|模拟|P1003 铺地毯