这个控件用到的几率非常大,如何利用自定义布局实现?
<com.android.UI.myListView android:id="@+id/xx" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" icontext:TextSize="10sp" android:padding="1dp" > </com.android.UI.myListView>类myListView实现:
@SuppressLint("NewApi") public class myListView extends LinearLayout//******继承LinearLayout类
{ private TextView editText;//******textview放内容 private ImageView imageView;//******imageView为最右箭头 private PopupWindow popupWindow = null;//******popupWindow为下拉框
private ArrayList<TextView> dataList = new ArrayList<TextView>(); private View mView;//********linearLayout的三个构造方法
public myListView(Context context) { this(context, null); // TODO Auto-generated constructor stub } public myListView(Context context, AttributeSet attrs) { this(context, attrs, 0); // TODO Auto-generated constructor stub } public myListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub initView();
//自定义的属性。首先要创建变量,创建了个values/attrs.xml文件,文件名任意,但是要在values目录下:
//TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.style);FontSize = a.getDimension(R.styleable.style_TextSize,16);//将获取自定义textSize的值,如果没有,则使用默认的值16。SetFontSize(FontSize);}
private void SetFontSize(Float s) { FontSize=k; editText.setTextSize(s); }attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="style"> <attr name="TextSize" format="dimension"/> </declare-styleable> </resources>
View downView; public void initView() { LayoutInflater layoutInflater; layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); downView= layoutInflater.inflate(R.layout.downlist_view, this); editText = (TextView) findViewById(R.id.text); editText.setTextSize(FontSize); imageView = (ImageView) findViewById(R.id.btn); this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (popupWindow == null) { showPopWindow();//*******打开下拉列表弹窗 } else { closePopWindow(); } } }); }downlist_view.xml文件:
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/compound" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/dropdown_bg_selector" android:stretchColumns="0" android:orientation="horizontal" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/text" android:layout_height="25dp" android:gravity="center_vertical" android:minWidth="100dp" android:paddingLeft="5dp" android:singleLine="true" android:text="" android:textSize="9dp"/> <ImageView android:id="@+id/btn" android:layout_width="20dp" android:layout_height="20dp" android:gravity="center" android:padding="5dp" android:src="@drawable/dropdown" /> </TableRow> </TableLayout>/** * 打开下拉列表弹窗 */ private void showPopWindow() { // 加载popupWindow的布局文件 LayoutInflater layoutInflater; layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View contentView = layoutInflater.inflate(R.layout.downlist_popupwindow, null, false); ListView listView = (ListView) contentView.findViewById(R.id.listView); LayoutParams para1; para1 = (LayoutParams) listView.getLayoutParams(); para1.width =downview.getWidth();//******下拉列表与起始同宽度 listView.setLayoutParams(para1);if (dataList != null) { listView.setAdapter(new myListAdapter(getContext(), dataList));//******若下拉列表有值,则设置适配器 } popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.tttt)); popupWindow.setOutsideTouchable(true); popupWindow.showAsDropDown(this); } /** * 关闭下拉列表弹窗 */ private void closePopWindow() { popupWindow.dismiss(); popupWindow = null; }downlist_popupwindow.xml:
<?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="match_parent" android:background="#666666" android:orientation="vertical" > <ListView android:id="@+id/listView" android:layout_width="20dp" android:layout_height="wrap_content" android:divider="#666666" android:padding="1dp" android:dividerHeight="1dp" > </ListView> </LinearLayout>R.color.tttt
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="tttt">#00000000</color> </resources>