How to make the distance between left edge of Toolbar and Search Icon same as that between the right edge of the toolbar and the Cancel Icon?

十年热恋 提交于 2020-01-28 02:45:06

问题


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:

  1. 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?

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

回答1:


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"


来源:https://stackoverflow.com/questions/31994512/how-to-make-the-distance-between-left-edge-of-toolbar-and-search-icon-same-as-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!