我在操作栏中有一个搜索小部件。我已经设置了一个android:hint值,但是当我点击搜索图标时,它不会显示出来。
相关文件:
MainActivity.java
package com.example.myApp;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.widget.SearchView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creates an action bar.
ActionBar actionBar = getActionBar();
//sets the homebutton.
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
}
Main.xml => res/menu/Main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView"
/>
</menu>
searchable.xml => res/XML/seearble.XMl
<?xml version="1.0" encoding="UTF-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="search"
android:textColorHint= "#FF0000"
/>
Mainfest.XMl
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.MyApp.MainActivity"
android:label="@string/app_name" >
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我的尝试是:
我的操作栏有一个黑色的背景,所以我假设我最初看不到:hint值,因为提示文本颜色也是黑色的。但是,即使在更改hintTextColor之后,我仍然看不到提示值。
有什么想法吗?
谢谢。
发布于 2014-01-08 21:10:18
出于某些原因,您需要将<action android:name="android.intent.action.SEARCH"/>
放入您的标记中。这就是为我解决问题的方法。但请注意,请按以下顺序排列
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
否则您的活动将不会被识别为启动器
发布于 2014-03-04 01:16:23
您必须为hint属性提供字符串资源。硬编码的文本不起作用。而不是
android:hint="search"
使用字符串资源
android:hint="@string/search_hint"
文档对此很清楚:http://developer.android.com/guide/topics/search/searchable-config.html
android:hint="string resource"
发布于 2014-02-10 14:53:16
您可以使用
android:queryHint="Hint text"
在XML格式的搜索小部件中。
https://stackoverflow.com/questions/20082535
复制相似问题