android在同一活动中添加两个工具栏? [英] android add two toolbars in the same activity ?

查看:90
本文介绍了android在同一活动中添加两个工具栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个底部菜单作为工具栏,我需要在顶部添加另一个菜单作为操作栏.这是活动代码

I have a bottom menu as a toolbar and I need to add another one at the top as an actionbar. here is the code ofthe activity

public class ListViewPharms extends AppCompatActivity {
public Toolbar mToolbar;
ListView mListView;
SimpleAdapter pharmAdapter;
LinearLayout searchsec;
// Search EditText
EditText inputSearch;


@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_action_bar);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.activity_list_view_pharms_title);



    Toolbar toolbar = (Toolbar) findViewById(R.id.mytoolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    setupEvenlyDistributedToolbar();



    inputSearch = (EditText) findViewById(R.id.search_bar);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            ListViewPharms.this.pharmAdapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.menu_register, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_home:
            Intent intent = new Intent(this, ListViewPharms.class);
            startActivity(intent);
            break;
        case R.id.action_cont:
            item.setIcon(getResources().getDrawable(R.drawable.icon2_active));

            Intent intent2 = new Intent(this, Contribute.class);
            startActivity(intent2);
            break;
        case R.id.action_info:
            Intent intent3 = new Intent(this, info.class);
            startActivity(intent3);
            break;
        case R.id.action_settings:
            Intent intent4 = new Intent(this, contactInfo.class);
            startActivity(intent4);
            break;
    }
    return true;
}


/**
 * This method will take however many items you have in your
 * menu/menu_main.xml and distribute them across your devices screen
 * evenly using a Toolbar. Enjoy!!
 */
public void setupEvenlyDistributedToolbar(){
    // Use Display metrics to get Screen Dimensions
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);

    // Toolbar
    mToolbar = (Toolbar) findViewById(R.id.mytoolbar);
    // Inflate your menu
    mToolbar.inflateMenu(R.menu.menu_register);

    // Add 10 spacing on either side of the toolbar
    mToolbar.setContentInsetsAbsolute(4, 4);

    // Get the ChildCount of your Toolbar, this should only be 1
    int childCount = mToolbar.getChildCount();
    // Get the Screen Width in pixels
    int screenWidth = metrics.widthPixels;

    // Create the Toolbar Params based on the screenWidth
    Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, ViewGroup.LayoutParams.WRAP_CONTENT);

    // Loop through the child Items
    for(int i = 0; i < childCount; i++){
        // Get the item at the current index
        View childView = mToolbar.getChildAt(i);
        // If its a ViewGroup
        if(childView instanceof ViewGroup){
            // Set its layout params
            childView.setLayoutParams(toolbarParams);
            // Get the child count of this view group, and compute the item widths based on this count & screen size
            int innerChildCount = ((ViewGroup) childView).getChildCount();
            int itemWidth  = (screenWidth / innerChildCount);
            // Create layout params for the ActionMenuView
            ActionMenuView.LayoutParams params = new ActionMenuView.LayoutParams(itemWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
            // Loop through the children
            for(int j = 0; j < innerChildCount; j++){
                View grandChild = ((ViewGroup) childView).getChildAt(j);
                if(grandChild instanceof ActionMenuItemView){
                    // set the layout parameters on each View
                    grandChild.setLayoutParams(params);
                }
            }
            }
        }
    }
}

这是此活动的xml,mytoolbar是底部的工具栏:

and here is the xml for this activity mytoolbar is the bottom toolbar:

<RelativeLayout
    android:id="@+id/pharms_list"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_above="@+id/mytoolbar">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"

        android:drawSelectorOnTop="false"
        android:layout_below="@+id/first_sec"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

<android.support.v7.widget.Toolbar
    android:id="@+id/mytoolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@drawable/linesup"
    app:contentInsetLeft="10dp"
    app:contentInsetRight="10dp"
    app:contentInsetStart="10dp"
    >
</android.support.v7.widget.Toolbar>

在清单文件中,我确实添加了android:uiOptions ="splitActionBarWhenNarrow"如何将菜单作为该图像添加到顶部?

and in the manifest file I did add the android:uiOptions="splitActionBarWhenNarrow" how I could add a menu to the top as this image?

推荐答案

我也有类似的需求,唯一的区别是我希望在顶部有一个工具栏,在底部有一个操作栏.

I had a similar need, the only difference being that I wanted a toolbar on top, and a sort of action bar at the bottom.

toobar必须与材料兼容,带有标题,一些按钮,并且可能有动作溢出,而底部的栏中必须包含一些等距且始终可见的按钮,仅此而已.

The toobar had to be material-compliant, with a title, some buttons, and possibly an action overflow, while the bottom bar had to contain some equally-spaced, always visible buttons, and nothing else.

我的实现在Android 4.x和Android 5.x上均经过测试并可以正常工作.

My implementation follows, tested and working, both on Android 4.x and Android 5.x.

这里是布局;如您所见,我不在底部栏中使用工具栏,而是在其内部使用的ActionMenuView,这是一个较低级的小部件,

Here's the layout; as you can see, I'm not using Toolbar for the bottom bar, but an ActionMenuView, which is a lower-level widget, that the Toolbar itself uses internally:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/page_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"/>

    <android.support.v7.widget.ActionMenuView
        android:id="@+id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentBottom="true"
        android:background="?attr/colorPrimary"
        android:elevation="4dp" />

    <WebView
        android:id="@+id/page"
        android:layout_below="@id/page_toolbar"
        android:layout_above="@id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

这是底部栏的菜单;与常规工具栏菜单相比没有什么不同:

Here's the menu for the bottom bar; nothing different, compared to a regular toolbar menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_list"
        android:icon="@drawable/ic_list"
        android:title="@string/action_list"
        app:showAsAction="always"/>

    <!-- several more items here... -->

</menu>

这是我的活动中的代码,该代码源自AppCompatActivity;如您所见,我也利用顶部工具栏的onCreateOptionsMenu回调来膨胀和初始化底部栏.不幸的是,底部栏菜单项的侦听器需要单独设置.但是它们可以与顶部工具栏共享相同的onOptionsItemSelected:

And here's the code in my activity, that derives from AppCompatActivity; as you can see I exploit the onCreateOptionsMenu callback of the top toolbar to inflate and initialize the bottom bar too. Unfortunately, listeners for bottom bar menu items need to be set individually. But they can share the same onOptionsItemSelected with the top toolbar:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the toolbar menu
    getMenuInflater().inflate(R.menu.web_page_menu, menu);

    // Inflate and initialize the bottom menu
    ActionMenuView bottomBar = (ActionMenuView)findViewById(R.id.bottom_toolbar);
    Menu bottomMenu = bottomBar.getMenu();
    getMenuInflater().inflate(R.menu.obj_menu, bottomMenu);
    for (int i = 0; i < bottomMenu.size(); i++) {
        bottomMenu.getItem(i).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return onOptionsItemSelected(item);
            }
        });
    }

    return true;
}

屏幕截图:

我不确定这是否是完全正统的实现,但我希望它对某人有用.

I am not sure this is a totally orthodox implementation, but I hope it can be useful to someone.

这篇关于android在同一活动中添加两个工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆