Android: SearchView使用细节

一.XML配置

<SearchView
                android:id="@+id/device_search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:gravity="center_vertical"
                android:focusable="false"
                android:focusableInTouchMode="true"
                android:iconifiedByDefault="false"
                android:queryHint="输入名称或者IMEI,搜索设备"
                android:background="@drawable/square_search_bg"/>

二.代码配置

	private void initSearchView() {
		//去掉搜索框下划线
		try {
			Class<? extends SearchView> aClass = mDeviceSearchView.getClass();
			Field searchFiled = aClass.getDeclaredField("mSearchPlate");
			searchFiled.setAccessible(true);
			View mView = (View) searchFiled.get(mDeviceSearchView);
			mView.setBackgroundColor(Color.TRANSPARENT);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//设置输入字体颜色
		int id = mDeviceSearchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
		et_search = mDeviceSearchView.findViewById(id);
		et_search.setTextColor(getResources().getColor(R.color.title_bar));//字体颜色
		et_search.setTextSize(15);//字体、提示字体大小
		et_search.setBackgroundResource(R.drawable.bg_search_selector);
//      textView.setHintTextColor(Color.BLUE);//提示字体颜色

		//将搜索图标设置为不显示
//        int searchId = mDeviceSearchView.getContext().getResources().getIdentifier("android:id/search_mag_icon", null, null);
//        ImageView ivSearchIcon = (ImageView) mDeviceSearchView.findViewById(searchId);
//        ivSearchIcon.setVisibility(View.GONE);
	}

猜你喜欢

转载自blog.csdn.net/mawei7510/article/details/82383488
今日推荐