首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >10折叠操作视图

10折叠操作视图
EN

Stack Overflow用户
提问于 2014-03-24 21:38:30
回答 1查看 3.4K关注 0票数 5

我目前正在开发需要支持Api10+的Android应用程序,我遵循了许多教程来设置appcompat来管理创建操作栏。

链接:https://stackoverflow.com/a/21291156/2789106 http://developer.android.com/guide/topics/ui/actionbar.html

只有三件事我不能做。

  1. 当搜索打开时,搜索放大镜不会更改为“可绘制”设置。
  2. 我无法更改"back“图标( home按钮,留置到徽标上(这是一个与我在创建drawerToggle时传递的图像不同的图像))
  3. 单击键盘搜索时,我无法以编程方式关闭(折叠)操作视图。(调用collapseActionView()是在API 14中实现的)

这是我的密码:

代码语言:javascript
复制
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_navigation_drawer,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

    };

    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    // Restore app state if any
    if (savedInstanceState == null) {
        mLeftMenuContainer = (LinearLayout) findViewById(R.id.left_menu_container);
        mLeftMenuContainer.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // return TRUE to avoid tap on back view
                return true;
            }

        });
        mDrawerLayout.closeDrawer(mLeftMenuContainer);

    }

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    getSupportActionBar().setLogo(R.drawable.ic_action_bar_logo);

}

/* Called whenever we call supportInvalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the nav drawer is open, hide action items related to the content
    // view
    if (mDrawerLayout != null && mLeftMenuContainer != null) {
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mLeftMenuContainer);
        for (int i = 0; i < menu.size(); i++) {
            menu.getItem(i).setVisible(!drawerOpen);
        }
    }

    return super.onPrepareOptionsMenu(menu);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);  
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    mMenu = menu;
    // Inflate the menu; this adds items to the action bar if it is present.
    // enable ActionBar app icon to behave as action to toggle nav drawer
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem searchItem = menu.findItem(R.id.action_bar_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchableInfo info = searchManager.getSearchableInfo(getComponentName());

    searchView.setSearchableInfo(info);  

    AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchText.setHintTextColor(getResources().getColor(R.color.color_action_bar_search_text));
    searchText.setTextColor(getResources().getColor(R.color.color_action_bar_search_text));

    searchView.setIconifiedByDefault(true);

    // Getting the 'search_plate' LinearLayout.
    View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    // Setting background of 'search_plate' to personal defined drawable.
    if (searchPlate != null) {
        searchPlate
            .setBackgroundResource(R.drawable.texfield_search_view_theme);
    }

    // Set search view clear icon
    ImageView searchIconClearView = (ImageView) searchView
            .findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    if (searchIconClearView != null) {
        Log.v(LOG_TAG, "Should Change Clear Icon here");

        searchIconClearView
            .setImageResource(R.drawable.ic_action_bar_clear_search);

    }

    // Set search view Magnifier icon
    ImageView searchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
    if (searchIcon != null) {
        Log.v(LOG_TAG, "Should Change Search Icon here");
        searchIcon.setImageResource(R.drawable.ic_action_bar_back);
    }

    // Set on click to open a fragment, not a activity
    final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // Do something
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            Log.v(LOG_TAG, "Performed search with: " + query);
            searchView.clearFocus();
            return true;
        }
    };

    searchView.setOnQueryTextListener(queryTextListener);

    MenuItemCompat.setOnActionExpandListener(searchItem, new OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionCollapse(MenuItem arg0) {
            mMenu.findItem(R.id.action_bar_cart).setVisible(true);
            return true;
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem arg0) {
            getSupportActionBar().setIcon(R.drawable.ic_action_bar_logo);
            mMenu.findItem(R.id.action_bar_cart).setVisible(false);
            return true;
        }


    });

    return super.onCreateOptionsMenu(menu);
}

有人能告诉我我做错什么了吗?

-编辑--

我设法解决了问题1和问题2:

您需要做的是在主题中定义homeUpIcon。因此,如果您像我一样支持API 10:

代码语言:javascript
复制
<style name="Theme.MyTheme" parent="@style/Theme.AppCompat.Light">
    <item name="searchViewSearchIcon">@drawable/your_search_icon</item>

    <!-- API 13- Support -->
    <item name="homeAsUpIndicator">@drawable/your_back_icon</item> 

    <!-- API 14+  -->
    <item name="android:homeAsUpIndicator">@drawable/your_back_icon</item> 

</style>

找到位置

ActionBarSherlock: changing homeAsUpIndicator doesn't work

Changing the background drawable of the searchview widget

最重要的问题仍然悬而未决,如果有人知道答案,请张贴在这里!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-01 16:53:28

试试这个:

代码语言:javascript
复制
   public boolean onQueryTextSubmit(String query) {
        MenuItemCompat.collapseActionView(searchItem);
        return false;
    }
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22621122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档