AutoCompleteTextView下拉列表字体颜色

问题描述:

在开发当中,一旦你开发的项目样式更改,会给整个项目带来潜在的bug。如AutoCompleteTextView下拉字体颜色

在配置文件的sdk配置为:

 <uses-sdk
        android:minSdkVersion="8"
         />
可以正常显示,但是在将其更改为

 <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="13" />

问题示例如下:


同样的问题,可以见stackoverflow:http://stackoverflow.com/questions/11787057/autocompletetextview-background-foreground-color

问题代码如下:

public class WikiSuggestActivity extends Activity {
	public String data;
	public List<String> suggest;
	public AutoCompleteTextView autoComplete;
	public ArrayAdapter<String> aAdapter;

	private static final String TAG = "WikiSuggestActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		suggest = new ArrayList<String>();
		autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
		autoComplete.setOnItemClickListener(itemClickListener);
		autoComplete.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable editable) {

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String newText = s.toString();
				new getJson().execute(newText);
			}

		});
	}

	OnItemClickListener itemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			int key = parent.getId();
			Log.d(TAG, "key is " + key + " and R.id.autoCompleteTextView1 is "
					+ R.id.autoCompleteTextView1);
			switch (key) {
			case R.id.autoCompleteTextView1:
				Log.d(TAG, "I am magic in place!");
				break;
			}

		}

	};

	class getJson extends AsyncTask<String, String, String> {

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			aAdapter = new ArrayAdapter<String>(getApplicationContext(),
					android.R.layout.simple_dropdown_item_1line, suggest);
			autoComplete.setAdapter(aAdapter);
			aAdapter.notifyDataSetChanged();
		}

		@Override
		protected String doInBackground(String... key) {
			String newText = key[0];
			newText = newText.trim();
			newText = newText.replace(" ", "+");
			try {
				HttpClient hClient = new DefaultHttpClient();
				HttpGet hGet = new HttpGet(
						"http://en.wikipedia.org/w/api.php?action=opensearch&search="
								+ newText + "&limit=8&namespace=0&format=json");
				ResponseHandler<String> rHandler = new BasicResponseHandler();
				data = hClient.execute(hGet, rHandler);
				suggest = new ArrayList<String>();
				JSONArray jArray = new JSONArray(data);
				for (int i = 0; i < jArray.getJSONArray(1).length(); i++) {
					String SuggestKey = jArray.getJSONArray(1).getString(i);
					suggest.add(SuggestKey);
				}

			} catch (Exception e) {
				Log.w("Error", e.getMessage());
			}
			return null;
		}

	}
}

解决方案:

编写simple_dropdown_item_1line.xml文件
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:gravity="left"
    android:paddingBottom="5dip"
    android:paddingTop="5dip"
    android:singleLine="true"
    android:textColor="#ff000000"
    android:textSize="22sp" />
修改Java代码
aAdapter = new ArrayAdapter<String>(getApplicationContext(),
					android.R.layout.simple_dropdown_item_1line, suggest);
为:
aAdapter = new ArrayAdapter<String>(getApplicationContext(),
					R.layout.simple_dropdown_item_1line, suggest);
效果如下:






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值