问题描述
我想添加一个搜索查看来材料设计工具栏 / 动作条,使得搜索查看默认情况下会扩大,占据了整个空间的工具栏宽度 - 明智。我有以下工具栏为止。
与它的问题是:的
-
我要搜索图标有从屏幕的左边缘的距离相同的的 X 图标从屏幕右边缘。 我怎么能这样做?
-
X 图标只有当我开始输入搜索查询,然后消失,如果我取消由pressing它的查询显示。我想这是有尽快工具栏显示(这只要活动似乎是),而只是停留,即使没有搜索查询(按类型用户)有与搜索提示文本是可见的。 我怎么能这样做?
我的尝试:的
我尝试添加安卓contentInsetStart ,安卓contentInsetLeft ,应用程序:contentInsetStart 和应用:contentInsetLeft 到工具栏 XML,这并没有帮助。然后我试图找到工具栏(通过findViewById())从的onCreate()的活动,然后做了 toolbar.setContentInsetsAbsolute(0,0 ); 。 无论是帮助。
然后我试图加入 setSupportActionBar(工具栏)来的onCreate()和 ActionBar.LayoutParams PARAMS =新ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT ,ActionBar.LayoutParams.MATCH_PARENT); searchView.setLayoutParams(PARAMS); 在onCreateOptionsMenu(),但不成功
SSCCE code:
MainActivity.java
公共类MainActivity扩展AppCompatActivity { 私人的TextView TextView的; 私人工具条工具栏; @覆盖 保护无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main); TextView的=(的TextView)findViewById(R.id.mainActivity_textView); 工具栏=(栏)findViewById(R.id.mainActivity_toolbar); setSupportActionBar(工具栏); toolbar.setContentInsetsAbsolute(0,0); handleIntent(getIntent()); } @覆盖 保护无效onNewIntent(意向意图){ setIntent(意向); handleIntent(意向); } 私人无效handleIntent(意向意图){ 如果(Intent.ACTION_SEARCH.equals(intent.getAction())){ 查询字符串= intent.getStringExtra(SearchManager.QUERY); doMySearch(查询); } } 私人无效doMySearch(字符串searchQuery){ textView.setText(searchQuery); } @覆盖 公共布尔onCreateOptionsMenu(功能菜单){ //充气菜单;这增加了项目操作栏,如果它是present。 。getMenuInflater()膨胀(R.menu.main,菜单); //充气从XML选项菜单 MenuInflater充气= getMenuInflater(); inflater.inflate(R.menu.main,菜单); //获取搜索查看,并设置了搜索的配置 SearchManager searchManager =(SearchManager)getSystemService(Context.SEARCH_SERVICE); 菜单项searchMenuItem = menu.findItem(R.id.action_search); 搜索查看搜索查看=(搜索查看)MenuItemCompat.getActionView(searchMenuItem); //假设当前的活动是搜索活动 searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); //searchView.setIconifiedByDefault(false); //不要图标化的工具;默认情况下,展开 返回true; } @覆盖 公共布尔onOptionsItemSelected(菜单项项){ //处理动作栏项目点击这里。将操作栏 //自动在主/向上按钮操作的点击,只要 //你在AndroidManifest.xml中指定一个父活动。 INT的id = item.getItemId(); 返回super.onOptionsItemSelected(项目); } }
activity_main.xml
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android 的xmlns:工具=http://schemas.android.com/tools 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:paddingBottom会=@扪/ activity_vertical_margin 机器人:以下属性来=@扪/ activity_horizontal_margin 机器人:paddingRight =@扪/ activity_horizontal_margin 机器人:paddingTop =@扪/ activity_vertical_margin 工具:上下文=practice_projects.materialdesigngooglenowlikesearchviewgive.MainActivity> <包括机器人:ID =@ + ID / mainActivity_toolbar 布局=@布局/ app_bar/> <的TextView 机器人:ID =@ + ID / mainActivity_textView 机器人:layout_below =@ ID / mainActivity_toolbar 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:文本=@字符串/参考hello world/> < / RelativeLayout的>
app_bar.xml
< XML版本=1.0编码=UTF-8&GT?; < android.support.v7.widget.Toolbar的xmlns:机器人=http://schemas.android.com/apk/res/android的xmlns:程序=http://schemas.android.com/apk/res -汽车 机器人:layout_width =match_parent 机器人:layout_height =WRAP_CONTENT 机器人:contentIntentStart =0dp 机器人:contentIntentLeft =0dp 应用程序:contentIntentStart =0dp 应用程序:contentIntentLeft =0dp> < /android.support.v7.widget.Toolbar>
RES /菜单/ main.xml中
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android 的xmlns:程序=http://schemas.android.com/apk/res-auto 的xmlns:工具=http://schemas.android.com/tools 工具:上下文=practice_projects.materialdesigngooglenowlikesearchviewgive.MainActivity> <项目 机器人:ID =@ + ID / ACTION_SEARCH 机器人:orderInCategory =100 机器人:标题=@字符串/ searchMenuIcon_title 机器人:图标=@可绘制/ ic_search_black_24dp 应用程序:showAsAction =总是 应用程序:actionViewClass =android.support.v7.widget.SearchView 应用程序:iconifiedByDefault =FALSE/> < /菜单>
RES / XML / searchable.xml
< XML版本=1.0编码=UTF-8&GT?; <可搜索的xmlns:机器人=http://schemas.android.com/apk/res/android 机器人:标签=@字符串/ APP_NAME 机器人:提示=@字符串/ search_hint> < /搜索>
RES /价值/ styles.xml
<资源> <样式名称=AppBaseTheme父=Theme.AppCompat.Light> <项目名称=colorPrimary> @色/初级< /项目> <项目名称=colorPrimaryDark> @色/ primaryDark< /项目> <项目名称=colorAccent> @颜色/重音和LT; /项目> < /风格> <样式名称=AppTheme父=AppBaseTheme>< /风格> < /资源>
RES /值* -v21 * / styles.xml
< XML版本=1.0编码=UTF-8&GT?; <资源> <样式名称=AppTheme父=AppBaseTheme>< /风格> < /资源>
我相信
安卓contentIntentStart =0dp 机器人:contentIntentLeft =0dp 应用程序:contentIntentStart =0dp 应用程序:contentIntentLeft =0dp
的意思是
安卓contentInsetStart =0dp 机器人:contentInsetLeft =0dp 应用程序:contentInsetStart =0dp 应用程序:contentInsetLeft =0dp
I am trying to add a SearchView to Material Design Toolbar/ActionBar such that the SearchView is expanded by default and takes up the entire space of the Toolbar width-wise. I have the following Toolbar so far.
The problem with it is:
I want the Search icon to have the same distance from the left edge of the screen as that of the X icon from the right edge of the screen. How can I do that?
The X icon appears only when I start typing a search query, and then it disappears if I cancel the query by pressing it. I want it to be there as soon as the Toolbar appears (which is as soon as the Activity appears), and just stay even when there is no search-query (typed by the user) there and the search hint text is visible. How can I do that?
WHAT I TRIED:
I tried to add android:contentInsetStart, android:contentInsetLeft, app:contentInsetStart and app:contentInsetLeft to the Toolbar XML, which did not help. Then I tried to find the Toolbar (by findViewById()) from onCreate() of the Activity, and then did toolbar.setContentInsetsAbsolute(0,0);. Neither helped.
Then I tried adding setSupportActionBar(Toolbar) to onCreate() and ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT); searchView.setLayoutParams(params); in onCreateOptionsMenu(), but in vain.
SSCCE CODE:
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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 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" > <include android:id="@+id/mainActivity_toolbar" layout="@layout/app_bar"/> <TextView 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" /> </RelativeLayout>
app_bar.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:contentIntentStart="0dp" android:contentIntentLeft="0dp" app:contentIntentStart="0dp" app:contentIntentLeft="0dp" > </android.support.v7.widget.Toolbar>
res/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="practice_projects.materialdesigngooglenowlikesearchviewgive.MainActivity" > <item 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" /> </menu>
res/xml/searchable.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="@string/search_hint" > </searchable>
res/values/styles.xml
<resources> <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primaryDark</item> <item name="colorAccent">@color/accent</item> </style> <style name="AppTheme" parent="AppBaseTheme"></style> </resources>
res/values*-v21*/styles.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppTheme" parent="AppBaseTheme"></style> </resources>
I believe that
android:contentIntentStart="0dp" android:contentIntentLeft="0dp" app:contentIntentStart="0dp" app:contentIntentLeft="0dp"
was meant to be
android:contentInsetStart="0dp" android:contentInsetLeft="0dp" app:contentInsetStart="0dp" app:contentInsetLeft="0dp"
这篇关于如何使工具栏的左边缘之间的距离,搜索图标相同的工具栏的右边缘与取消图标之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!