android Toolbar 의 간단 한 사용

6038 단어 androidToolBar
본 논문 의 사례 는 여러분 에 게 ToolBar 의 사용 방법 을 공유 하 였 으 며,여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

도구 모음 에 사용 할 표준 도구 모음;Action Bar 대신 사용 하기;
ToolBar 를 사용 하려 면 Activity 설정 theme 에서 ActionBar 를 제거 해 야 합 니 다.예 를 들 어 사용 합 니 다.

Theme.AppCompat.Light.NoActionBar 
또는 테마 style 에서 스타일 사용자 정의:

<style name="AppThemeNoColour" parent="Theme.AppCompat.Light.NoActionBar"> 
        <!--       ActionBar   title;      ,            --> 
        <item name="android:windowActionBar">false</item> 
        <item name="android:windowNoTitle">true</item> 
        <!--       --> 
        <item name="colorPrimaryDark">@android:color/black</item> 
        <!--         --> 
        <item name="android:windowBackground">@android:color/white</item> 
        <item name="colorAccent">@color/colorAccent</item> 
        <item name="android:windowDrawsSystemBarBackgrounds">false</item> 
    </style> 
다음은 레이아웃 을 살 펴 보 겠 습 니 다.

<LinearLayout 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" 
  tools:context="www.toolbar.com.toolbardemo.MainActivity" 
  android:orientation="vertical"> 
 
  <android.support.v7.widget.Toolbar 
    android:layout_width="match_parent" 
    android:layout_height="56dp" 
    android:id="@+id/toolbar" 
    android:background="@android:color/holo_blue_dark" 
    /> 
 
</LinearLayout> 
Toolbar 는 리 턴 키,아이콘,제목,부제,메뉴 를 설정 할 수 있 습 니 다.

Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar); 
 
//     Toolbar  ActionBar 
setSupportActionBar(toolbar); 
//     
toolbar.setLogo(R.mipmap.ic_launcher); 
//     
getSupportActionBar().setTitle("ZhengDang"); 
//      
toolbar.setSubtitle("2015.01.13"); 
//      
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
리 턴 키 는 감청 설정 도 가능 합 니 다:

toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        Toast.makeText(MainActivity.this,"        ",Toast.LENGTH_SHORT).show(); 
 
      } 
    }); 
설정 메뉴 는 먼저 menu 레이아웃 을 사용자 정의 해 야 합 니 다:res--->menu 에서 xml 파일 을 만 듭 니 다:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:app="http://schemas.android.com/apk/res-auto"> 
 
  <!-- 
    showAsAction: 
    ifRoom 
        Item ,       4   4    Item          。 
               4 ,          
 
    never 
          。          ,       ,     item   ,        。 
 
    always 
          ,    。 
 
    withText 
    withText   Action bar       。Action bar           , 
      ,          Action bar     ,           。 
 
    collapseActionView 
                        ,          , 
            。  ,                 ,             , 
                。 
    --> 
  <item 
    android:id="@+id/action_setting" 
    android:orderInCategory="100" 
    android:title="settings" 
    app:showAsAction="always" 
    android:icon="@drawable/scan_bg" 
    /> 
 
  <item 
    android:id="@+id/action_ufc" 
    android:orderInCategory="100" 
    android:title="UFC" 
    /> 
 
  <item 
    android:id="@+id/action_wlf" 
    android:orderInCategory="100" 
    android:title="   " 
    /> 
  <item 
    android:id="@+id/action_klf" 
    android:orderInCategory="100" 
    android:title="   " 
    /> 
</menu>
 그리고 MainActivity 에서 메뉴 단 추 를 설정 합 니 다.

/** 
   *        : 
   *           ,  menu          Menu  。   true    menu,false     ; 
   * (              ) 
   */ 
  public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main,menu); 
    return super.onCreateOptionsMenu(menu); 
  } 

//       :       
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { 
      @Override 
      public boolean onMenuItemClick(MenuItem item) { 
        switch (item.getItemId()){ 
          case R.id.action_wlf: 
            Toast.makeText(MainActivity.this,"         ",Toast.LENGTH_SHORT).show(); 
            break; 
          case R.id.action_klf: 
            Toast.makeText(MainActivity.this,"           ",Toast.LENGTH_SHORT).show(); 
            break; 
          case R.id.action_ufc: 
            Toast.makeText(MainActivity.this,"           ",Toast.LENGTH_SHORT).show(); 
            break; 
          case R.id.action_setting: 
            Toast.makeText(MainActivity.this,"   ",Toast.LENGTH_SHORT).show(); 
            break; 
        } 
        return false; 
      } 
    }); 
클릭 하여 링크 열기 무료원본 코드 다운로드
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기