欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ToolBar使用方法詳解

 更新時(shí)間:2017年08月04日 09:51:02   作者:mChenys  
這篇文章主要為大家詳細(xì)介紹了Android中ToolBar的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

ToolBar的出現(xiàn)是為了替換之前的ActionBar的各種不靈活使用方式,相反,ToolBar的使用變得非常靈活,因?yàn)樗梢宰屛覀冏杂赏锩嫣砑幼涌丶?低版本要使用的話,可以添加support-v7包.

今天要實(shí)現(xiàn)的效果如下:

這里寫圖片描述

由上圖可以看到,toolBar的布局還是相對豐富的.要使用toolBar,首先為了兼容低版本,需要在gradle中引入支持庫

compile 'com.android.support:appcompat-v7:23.4.0'


其次,我們還需要隱藏默認(rèn)的ActionBar,否則會報(bào)如下錯誤:

Caused by: java.lang.IllegalStateException: This Activity already has an action bar
supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set
windowActionBar to false in your theme to use a Toolbar instead.

可以在res/value/style.xml中設(shè)置:

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="AppTheme.Base"></style>

  <style name="AppTheme.Base" parent="Theme.AppCompat">
    <!--將ActionBar隱藏,這里使用ToolBar-->
    <item name="windowActionBar">false</item>
    <!-- 使用 API Level 22以上編譯的話,要拿掉前綴字 -->
    <item name="windowNoTitle">true</item>
    <!--colorPrimaryDark對應(yīng)狀態(tài)欄的顏色-->
    <item name="colorPrimaryDark">@color/statusColor</item>
    <!--colorPrimary 對應(yīng)ToolBar的顏色-->
    <item name="colorPrimary">@color/toolBarColor</item>
    <!--colorAccent 對應(yīng)EditText編輯時(shí)、RadioButton選中、CheckBox等選中時(shí)的顏色。-->
    <item name="colorAccent">@color/editColor</item>
    <!--窗口的顏色-->
    <item name="android:windowBackground">@color/widowColor</item>

  </style>

  <!--Status bar color-->
  <color name="statusColor">#ff0000</color>
  <!-- toolBar color -->
  <color name="toolBarColor">#0000ff</color>
  <!--EditText,RadioButton,CheckBox color-->
  <color name="editColor">#FD87A9</color>
  <!--Window color-->
  <color name="widowColor">#ffffff</color>

</resources>

從上面的style文件中,可以知道,手機(jī)狀態(tài)欄的顏色和ToolBar的顏色也是可以在style中配置的.然后在清單文件的application節(jié)點(diǎn)下需要確認(rèn)使用的style是Android:theme=”@style/AppTheme”

ok,樣式配置完后,接著在res/layout/activity_main.xml中加入Toolbar控件.

<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"
  android:orientation="vertical"
  tools:context=".MainActivity">

  <!--
  ?attr/actionBarSize:表示根據(jù)屏幕的分辨率采用系統(tǒng)默認(rèn)的高度
  如果低版本也要使用的話,則需要使用v7包的,否則只有api21上才能有效
  -->
  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary">
    <!--添加Toolbar的子控件-->
    <Button
      android:id="@+id/btn_diy"
      android:layout_width="60dp"
      android:layout_height="wrap_content"
      android:layout_gravity="right"
      android:background="#80ffffff"
      android:text="自定義按鈕"
      android:textColor="#000000"
      android:textSize="11sp" />

    <TextView
      android:id="@+id/tv_title"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="center"
      android:gravity="center"
      android:text="首頁"
      android:textColor="@android:color/black"
      android:textSize="20sp" />
  </android.support.v7.widget.Toolbar>

  <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/hello_world"
    android:textColor="@android:color/black"
    android:textSize="30sp" />


</LinearLayout>

再來看看MainActivity的代碼:

package blog.csdn.net.mchenys.toolbardemo;

import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.Toast;
/**
 * Created by mChenys on 2016/5/29.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  Toolbar mToolbar;
  Toast mToast;
  PopupWindow mPopupWindow;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    mToast.setGravity(Gravity.CENTER, 0, 0);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    // App Logo
//    mToolbar.setLogo(R.drawable.app_icon);
    // 主標(biāo)題,默認(rèn)為app_label的名字
    mToolbar.setTitle("Title");
    mToolbar.setTitleTextColor(Color.YELLOW);
    // 副標(biāo)題
    mToolbar.setSubtitle("Sub title");
    mToolbar.setSubtitleTextColor(Color.parseColor("#80ff0000"));
    //側(cè)邊欄的按鈕
    mToolbar.setNavigationIcon(R.drawable.back);
    //取代原本的actionbar
    setSupportActionBar(mToolbar);
    //設(shè)置NavigationIcon的點(diǎn)擊事件,需要放在setSupportActionBar之后設(shè)置才會生效,
    //因?yàn)閟etSupportActionBar里面也會setNavigationOnClickListener
    mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mToast.setText("click NavigationIcon");
        mToast.show();
      }
    });
    //設(shè)置toolBar上的MenuItem點(diǎn)擊事件
    mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
      @Override
      public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
          case R.id.action_edit:
            mToast.setText("click edit");
            break;
          case R.id.action_share:
            mToast.setText("click share");
            break;
          case R.id.action_overflow:
            //彈出自定義overflow
            popUpMyOverflow();
            return true;
        }
        mToast.show();
        return true;
      }
    });
    //ToolBar里面還可以包含子控件
    mToolbar.findViewById(R.id.btn_diy).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mToast.setText("點(diǎn)擊自定義按鈕");
        mToast.show();
      }
    });
    mToolbar.findViewById(R.id.tv_title).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        mToast.setText("點(diǎn)擊自定義標(biāo)題");
        mToast.show();
      }
    });
  }

  //如果有Menu,創(chuàng)建完后,系統(tǒng)會自動添加到ToolBar上
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
  }

  /**
   * 彈出自定義的popWindow
   */
  public void popUpMyOverflow() {
    //獲取狀態(tài)欄高度
    Rect frame = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    //狀態(tài)欄高度+toolbar的高度
    int yOffset = frame.top + mToolbar.getHeight();
    if (null == mPopupWindow) {
      //初始化PopupWindow的布局
      View popView = getLayoutInflater().inflate(R.layout.action_overflow_popwindow, null);
      //popView即popupWindow的布局,ture設(shè)置focusAble.
      mPopupWindow = new PopupWindow(popView,
          ViewGroup.LayoutParams.WRAP_CONTENT,
          ViewGroup.LayoutParams.WRAP_CONTENT, true);
      //必須設(shè)置BackgroundDrawable后setOutsideTouchable(true)才會有效
      mPopupWindow.setBackgroundDrawable(new ColorDrawable());
      //點(diǎn)擊外部關(guān)閉。
      mPopupWindow.setOutsideTouchable(true);
      //設(shè)置一個(gè)動畫。
      mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
      //設(shè)置Gravity,讓它顯示在右上角。
      mPopupWindow.showAtLocation(mToolbar, Gravity.RIGHT | Gravity.TOP, 0, yOffset);
      //設(shè)置item的點(diǎn)擊監(jiān)聽
      popView.findViewById(R.id.ll_item1).setOnClickListener(this);
      popView.findViewById(R.id.ll_item2).setOnClickListener(this);
      popView.findViewById(R.id.ll_item3).setOnClickListener(this);
    } else {
      mPopupWindow.showAtLocation(mToolbar, Gravity.RIGHT | Gravity.TOP, 0, yOffset);
    }

  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.ll_item1:
        mToast.setText("哈哈");
        break;
      case R.id.ll_item2:
        mToast.setText("呵呵");
        break;
      case R.id.ll_item3:
        mToast.setText("嘻嘻");
        break;
    }
    //點(diǎn)擊PopWindow的item后,關(guān)閉此PopWindow
    if (null != mPopupWindow && mPopupWindow.isShowing()) {
      mPopupWindow.dismiss();
    }
    mToast.show();
  }
}

另外,我們的ToolBar控件還用到了menu item,下面是/res/menu/menu_main.xml的內(nèi)容:

<?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"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context=".MainActivity">

  <item
    android:id="@+id/action_edit"
    android:icon="@drawable/edit"
    android:orderInCategory="80"
    android:title="@string/action_edit"
    app:showAsAction="ifRoom" />

  <item
    android:id="@+id/action_share"
    android:icon="@drawable/share"
    android:orderInCategory="90"
    android:title="@string/action_share"
    app:showAsAction="ifRoom" />

  <item
    android:id="@+id/action_overflow"
    android:orderInCategory="100"
    android:title="@string/action_more"
    android:icon="@drawable/more"
    app:showAsAction="always" />


</menu>

還有PopWindow的布局,在/res/layout/action_overflow_popwindow.xml,內(nèi)容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="#274B5E"
  android:orientation="vertical"
  android:padding="10dp">

  <LinearLayout
    android:id="@+id/ll_item1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/app_icon" />

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:text="哈哈"
      android:textColor="#ffffff"
      android:textSize="20sp" />
  </LinearLayout>

  <LinearLayout
    android:id="@+id/ll_item2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/app_icon" />

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:text="呵呵"
      android:textColor="#ffffff"
      android:textSize="20sp" />
  </LinearLayout>

  <LinearLayout
    android:id="@+id/ll_item3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/app_icon" />

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:text="嘻嘻"
      android:textColor="#ffffff"
      android:textSize="20sp" />
  </LinearLayout>
</LinearLayout>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中FontMetrics的幾個(gè)屬性全面講解

    Android中FontMetrics的幾個(gè)屬性全面講解

    下面小編就為大家?guī)硪黄狝ndroid中FontMetrics的幾個(gè)屬性全面講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-11-11
  • Android中使用RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載

    Android中使用RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載

    RecyclerView 是Android L版本中新添加的一個(gè)用來取代ListView的SDK,它的靈活性與可替代性比listview更好。這篇文章主要介紹了Android中使用RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • Android使用shape使組件呈現(xiàn)出特殊效果的方法

    Android使用shape使組件呈現(xiàn)出特殊效果的方法

    這篇文章主要介紹了Android使用shape使組件呈現(xiàn)出特殊效果的方法,結(jié)合實(shí)例形式分析了Android中shape文件相關(guān)屬性、功能及使用方法,需要的朋友可以參考下
    2017-07-07
  • Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小

    Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小

    這篇文章主要介紹了Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android應(yīng)用中實(shí)現(xiàn)跳轉(zhuǎn)外部瀏覽器打開鏈接功能

    Android應(yīng)用中實(shí)現(xiàn)跳轉(zhuǎn)外部瀏覽器打開鏈接功能

    在開發(fā)Android應(yīng)用程序時(shí),有時(shí)候我們需要讓用戶跳轉(zhuǎn)到外部瀏覽器打開特定的鏈接,例如打開一個(gè)網(wǎng)頁、下載文件等,本文將介紹如何在Android應(yīng)用中實(shí)現(xiàn)跳轉(zhuǎn)外部瀏覽器打開鏈接的功能,感興趣的朋友一起看看吧
    2024-06-06
  • Android自定義View實(shí)現(xiàn)柱狀波形圖的繪制

    Android自定義View實(shí)現(xiàn)柱狀波形圖的繪制

    柱狀波形圖是一種常見的圖形。一個(gè)個(gè)柱子按順序排列,構(gòu)成一個(gè)波形圖。本文將利用Android自定義View實(shí)現(xiàn)柱狀波形圖的繪制,需要的可以參考一下
    2022-08-08
  • Android6.0動態(tài)申請權(quán)限所遇到的問題小結(jié)

    Android6.0動態(tài)申請權(quán)限所遇到的問題小結(jié)

    這篇文章給大家介紹了Android6.0動態(tài)申請權(quán)限所遇到的問題,在沒給大家介紹這下問題之前,先給大家說下基本定義和基本使用方式,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,對android 6.0 動態(tài)權(quán)限遇到問題感興趣的朋友一起看看吧
    2016-11-11
  • Android自定義View仿華為圓形加載進(jìn)度條

    Android自定義View仿華為圓形加載進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android自定義View仿華為圓形加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android實(shí)現(xiàn)定時(shí)自動靜音小助手

    Android實(shí)現(xiàn)定時(shí)自動靜音小助手

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)自動靜音小助手,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • android獲取當(dāng)前運(yùn)行Activity名字的方法

    android獲取當(dāng)前運(yùn)行Activity名字的方法

    這篇文章主要介紹了android獲取當(dāng)前運(yùn)行Activity名字的方法,對比分析了兩種實(shí)現(xiàn)方法供大家選擇,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01

最新評論