android - 如何使工具栏左边缘与搜索图标之间的距离与工具栏右边缘与取消图标之间的距离相同?

标签 android searchview android-toolbar android-appcompat appcompat-v7-toolbar

我正在尝试将 SearchView 添加到 Material Design Toolbar/ActionBar 以便扩展 SearchView默认情况下,占据 Toolbar 宽度方向的整个空间。到目前为止,我有以下工具栏

enter image description here

问题是:

  1. 我希望“搜索”图标与屏幕左边缘的距离与 X 图标与屏幕右边缘的距离相同。 我该怎么做?

  2. X 图标仅在我开始输入搜索查询时出现,如果我按下该图标取消查询,它就会消失。我希望它在 Toolbar 出现后立即出现(在 Activity 出现后立即出现),并且即使那里没有搜索查询(由用户键入)也只是停留在那里并且搜索提示文本可见。 我该怎么做?

我尝试了什么:

我尝试添加 android:contentInsetStartandroid:contentInsetLeftapp:contentInsetStartapp:contentInsetLeftToolbar XML,这没有帮助。然后我尝试从 Activity 的 onCreate() 中找到 Toolbar(通过 findViewById()),然后执行 toolbar.setContentInsetsAbsolute(0,0);都没有帮助。

然后我尝试将 setSupportActionBar(Toolbar) 添加到 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

<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>

最佳答案

我相信

    android:contentIntentStart="0dp"
    android:contentIntentLeft="0dp"
    app:contentIntentStart="0dp"
    app:contentIntentLeft="0dp"

本来就是

    android:contentInsetStart="0dp"
    android:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"

关于android - 如何使工具栏左边缘与搜索图标之间的距离与工具栏右边缘与取消图标之间的距离相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31994512/

相关文章:

android 跨度点击事件

android - 将字符串传递给 webview 并显示

android - SearchView 隐藏 Toolbar 中的其他 Label

android - 多次设置 android 的 appBar/toolbar 标题的问题

c# - Xamarin Android 工具栏如何动态添加项目

android - 一种从 Android 应用程序追踪街道的方法?

java - 如何将html字符串传递给另一个 Activity webView

android - 当我点击它时,操作栏中的搜索 View 向左移动

java - 带有 SearchView 的 Android abs,onQueryTextListener 不工作

android - 更改由 xml 布局制作的工具栏菜单上的菜单项