android 侧边工具栏,android – 如何使工具栏左边缘和搜索图标之间的距离与工具栏右边缘和取消图标之间的距离相同?...
我正在尝试将SearchView添加到Material Design Toolbar / ActionBar,以便默认情况下扩展SearchView并占用工具栏的整个空间宽度.到目前为止,我有以下工具栏. 它的问题是:>我希望“搜索”图标与屏幕左边缘的距离与屏幕右边缘的X图标的距离相同.我怎样才能做到这一点?>只有当我开始键入搜索查询时,才会显示X图标,如果我通过按下它取消查询,它就会
我正在尝试将SearchView添加到Material Design Toolbar / ActionBar,以便默认情况下扩展SearchView并占用工具栏的整个空间宽度.到目前为止,我有以下工具栏.

它的问题是:
>我希望“搜索”图标与屏幕左边缘的距离与屏幕右边缘的X图标的距离相同.我怎样才能做到这一点?
>只有当我开始键入搜索查询时,才会显示X图标,如果我通过按下它取消查询,它就会消失.我希望它一出现工具栏就出现在那里(一旦Activity出现),即使没有搜索查询(由用户键入),并且搜索提示文本可见,我也希望它停留在那里.我怎样才能做到这一点?
我做了什么:
我试图将android:contentInsetStart,android:contentInsetLeft,app:contentInsetStart和app:contentInsetLeft添加到工具栏XML,这没有帮助.然后我尝试从Activity的onCreate()中找到工具栏(通过findViewById()),然后执行toolbar.setContentInsetsAbsolute(0,0);.没有帮助.
然后我尝试将setSupportActionBar(工具栏)添加到onCreate()和ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.MATCH_PARENT);
searchView.setLayoutParams(PARAMS);在onCreateOptionsMenu()中,但是徒劳无功.
SSCCE代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.mainActivity_textView);
toolbar = (Toolbar) findViewById(R.id.mainActivity_toolbar);
setSupportActionBar(toolbar);
toolbar.setContentInsetsAbsolute(0,0);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}
}
private void doMySearch(String searchQuery) {
textView.setText(searchQuery);
}
@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);
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
//searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
activity_main.xml中
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="practice_projects.materialdesigngooglenowlikesearchviewgive.MainActivity" >
layout="@layout/app_bar"/>
android:id="@+id/mainActivity_textView"
android:layout_below="@id/mainActivity_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
app_bar.xml
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentIntentStart="0dp"
android:contentIntentLeft="0dp"
app:contentIntentStart="0dp"
app:contentIntentLeft="0dp" >
RES /菜单/ main.xml中
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="practice_projects.materialdesigngooglenowlikesearchviewgive.MainActivity" >
android:id="@+id/action_search"
android:orderInCategory="100"
android:title="@string/searchMenuIcon_title"
android:icon="@drawable/ic_search_black_24dp"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
app:iconifiedByDefault="false" />
RES / XML / searchable.xml
android:label="@string/app_name"
android:hint="@string/search_hint" >
RES /值/ styles.xml
@color/primary
@color/primaryDark
@color/accent
RES /值* -v21 * / styles.xml
更多推荐



所有评论(0)