2015-09-27 151 views
5

Tôi có một menu dưới cùng làm thanh công cụ và tôi cần thêm một thanh công cụ khác ở trên cùng làm thanh tác vụ. đây là mã ofthe hoạt độngandroid thêm hai thanh công cụ trong cùng một hoạt động?

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); 
       } 
      } 
      } 
     } 
    } 
} 

và đây là xml cho mytoolbar hoạt động này là các thanh công cụ phía dưới:

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

và trong file manifest tôi đã thêm android: uiOptions = "splitActionBarWhenNarrow" làm thế nào tôi có thể thêm một trình đơn trên đầu trang như hình ảnh này?

enter image description here

Trả lời

3

Xem hướng dẫn sau đây Using the Android Toolbar (ActionBar) - Tutorial

Ngoài ra bạn có thể sử dụng hai thanh công cụ nếu bạn muốn có hai thanh công cụ khác nhau, chỉ cần đặt layout_gravity cả các thanh công cụ khác nhau, người ta có đầu và người khác có dưới cùng như sau:

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/coordinator" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 



    <!-- As the main content view, the view below consumes the entire 
     space available using match_parent in both dimensions. --> 
    <FrameLayout android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/circle_blue"/> 


    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_margin="8dp" 
     android:background="@drawable/curved_div_shadow" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:layout_scrollFlags="scroll|enterAlways"> 

    </android.support.v7.widget.Toolbar> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar2" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_margin="8dp" 
     android:layout_gravity="bottom" 
     android:background="@drawable/curved_div_shadow" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:layout_scrollFlags="scroll|enterAlways"> 

    </android.support.v7.widget.Toolbar> 

</android.support.design.widget.CoordinatorLayout> 

Cũng đọc này Using two toolbars with multiple menus để tăng các menu khác nhau cho cả hai thanh công cụ.

8

Tôi có nhu cầu tương tự, khác biệt duy nhất là tôi muốn một thanh công cụ ở trên cùng và một loại thanh tác vụ ở dưới cùng.

Các Thanh công cụ dành phải có vật liệu phù hợp, với một tiêu đề, một số nút, và có thể là một tràn hành động, trong khi thanh dưới cùng phải chứa một số bình đẳng-cách nhau, luôn luôn có thể nhìn thấy nút, và không có gì khác.

Thực hiện của tôi sau, được thử nghiệm và hoạt động, cả trên Android 4.x và Android 5.x.

Đây là bố cục; như bạn thấy đấy, tôi không sử dụng Thanh công cụ cho thanh dưới cùng, nhưng một ActionMenuView, mà là một widget cấp dưới, mà Thanh công cụ chính sử dụng trong nội bộ:

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

Dưới đây là menu cho thanh đáy; không có gì khác biệt, so với menu thanh công cụ thông thường:

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

Và đây là mã trong hoạt động của tôi, có nguồn gốc từ AppCompatActivity; như bạn có thể thấy tôi khai thác hàm callback onCreateOptionsMenu của thanh công cụ trên cùng để thổi phồng và khởi tạo thanh dưới cùng. Thật không may, người nghe cho các mục trình đơn thanh dưới cùng cần phải được đặt riêng lẻ. Nhưng họ có thể chia sẻ cùng onOptionsItemSelected với thanh công cụ trên:

@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; 
} 

Một ảnh chụp màn hình:

enter image description here

Tôi không chắc chắn đây là một việc thực hiện hoàn toàn chính thống, nhưng tôi hy vọng nó có thể hữu ích để người nào.

+0

Làm cách nào để các biểu tượng hiển thị đồng đều? Khi tôi sử dụng nó, tất cả các tùy chọn xuất hiện trong menu mục bổ sung, mặc dù showAsAction luôn xuất hiện. –

+0

Đừng bận tâm, có vẻ như bạn cần sử dụng getMenuInflator hơn là tạo một tài liệu với ngữ cảnh. –

Các vấn đề liên quan