荣耀彩票代理

  • 热门专题

Android中通过ActionBar为标题栏添加搜索以及分享视窗

作者:潘侯爷  发布日期:2016-12-26 20:23:34
  • Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果。ActionBar的主要目的是:

    1.TIGONGYIGEYONGYUSHIBIEYINGYONGCHENGXUDEBIAOSHIHEYONGHUDEWEIZHIDEZHUANYONGKONGJIAN。

    荣耀彩票代理2.ZAIBUTONGDEYINGYONGCHENGXUZHIJIANTIGONGYIZHIDEDAOHANGHESHIJUETIYAN。

    3.TUCHUActivityDEGUANJIANCAOZUO(RU“SOUSUO”、“CHUANGJIAN”、“GONGXIANG”DENG),BINGQIEZAIKEYUJIANDEFANGFANEIJIYONGHUTIGONGKUAIJIEDEFANGWEN。

    荣耀彩票代理JINTIANWOMENJIUJIANDANSHULIYIXIARUHETONGGUOActionBarWEIBIAOTITIANJIA“SOUSUO”HE“FENXIANG”DECAOZUOSHICHUANG,RUXIATU:

    CAOZUOSHICHUANGSHIZUOWEICAOZUOXIANGMUANNIUDETIDAIPINXIANSHIZAICAOZUOLANZHONGDEYIGEKESHIGOUJIAN。LIRU,RUGUONIYOUYIGEYONGYUSOUSUODEKEXUANCAIDANXIANG,NIKEYIYONGSearchviewLEILAITIDAICAOZUOLANSHANGDESOUSUOANNIU。

    YAOGECAIDANZIYUANZHONGDEYIGEXIANGMUSHENGMINGYIGECAOZUOSHICHUANG,NIJIKEYISHIYONGandroid:actionLayoutSHUXINGYEandroid:actionviewClassSHUXINGLAIFENBIEZHIDINGYIGEBUJUZIYUANHUOYAOSHIYONGDEKESHIGOUJIANLEI。

    YUCAOZUOSHICHUANGLEISI,CAOZUOTIGONGQI(YOUActionProviderLEIDINGYIDE)YONGYIGEDINGZHIDEBUJUDAITIYIGECAOZUOXIANGMU,TAHAIXUYAODUISUOYOUZHEIXIEXIANGMUXINGWEIDEKONGZHI。DANGNIZAICAOZUOLANZHONGJIYIGECAIDANXIANGSHENGMINGYIGECAOZUOXIANGMUSHI,TABUJINYAOYIGEDINGZHIDEBUJULAIKONGZHIZHEIGECAIDANXIANGDEWAIGUAN,ERQIEDANGTAZAIXIANSHIZAIYICHUCAIDANZHONGSHI,HAIYAOCHULITADEMORENSHIJIAN。WULUNSHIZAICAOZUOLANZHONGHAISHIZAIYICHUCAIDANZHONG,TADOUNENGGOUTIGONGYIGEZICAIDAN。LIRU,ActionProviderDEKUOZHANLEIShareActionProvider,TATONGGUOZAICAOZUOLANZHONGXIANSHIYIGEYOUXIAODEGONGXIANGMUBIAOLIEBIAOLAIFANGBIANGONGXIANGCAOZUO。YUSHIYONGCHUANTONGDEDIAOYONGACTION_SENDLEIXINGIntentDUIXIANGDECAOZUOXIANGBUTONG,NINENGGOUSHENGMINGYIGEShareActionProviderDUIXIANGLAICHULIYIGECAOZUOXIANG。ZHEIZHONGCAOZUOTIGONGQIHUIBAOLIUYIGEDAIYOUCHULIACTION_SENDDEIntentDUIXIANGDEYINGYONGCHENGXUDEXIALALIEBIAO,SHIZHEIGECAIDANXIANGXIANSHIZAIYICHUCAIDANZHONG。YINCI,DANGNISHIYONGXIANGZHEIYANGDECAOZUOTIGONGQISHI,NIBUBICHULIYOUGUANZHEIGECAIDANXIANGDEYONGHUSHIJIAN。YAOJIYIGECAOZUOXIANGSHENGMINGYIGECAOZUOTIGONGQI,JIUYAOZAICAIDANZIYUANZHONGDUIYINGDE<item>YUANSUZHONGDINGYIandroid:actionProviderClassSHUXING,TIGONGQIYAOSHIYONGWANZHENGDELEIMING。

    第一步:建立menu资源文件

    荣耀彩票代理JIANLImenuZIYUANWENJIAN,SHEZHISOUSUOHEFENXIANGDEITEMGONGNENGZUJIAN。

    (1)XINJIANmenuWENJIANJIA,RUTU

    荣耀彩票代理(2)ZAImenuZIYUANWENJIANJIAXIAJIANLIserch_share_menu.xmlWENJIAN(ZIDINGYIMINGCHENG)

    (3)serch_share_menu.xmlZUJIANBUSHUDAIMA:

     1 <?xml version='1.0' encoding='utf-8'?>
     2 <menu xmlns:android='http://schemas.android.com/apk/res/android'
     3     xmlns:app='http://schemas.android.com/apk/res-auto'>
     4     <item
     5         android:id='@+id/search'
     6         app:actionViewClass='android.support.v7.widget.SearchView'
     7         android:icon='@android:drawable/ic_menu_search'
     8         android:orderInCategory='100'
     9         android:title='搜索'
    10         app:showAsAction='always|collapseActionView' />
    11     <item
    12         android:id='@+id/share'
    13         android:icon='@android:drawable/ic_menu_share'
    14         android:orderInCategory='200'
    15         android:title='共享'
    16         app:actionProviderClass='android.support.v7.widget.ShareActionProvider'
    17         app:showAsAction='ifRoom' />
    18 </menu>

    第二步:java中功能实现代码MainActivity.java:

     1 import android.content.Intent;
     2 import android.support.v4.view.MenuItemCompat;
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.support.v7.widget.SearchView;
     6 import android.support.v7.widget.ShareActionProvider;
     7 import android.view.Gravity;
     8 import android.view.Menu;
     9 import android.view.MenuItem;
    10 import android.widget.Toast;
    11 /**
    12  * Created by panchengjia on 2016/12/23.
    13  */
    14 
    15 public class MainActivity extends AppCompatActivity {
    16     SearchView searchView;
    17     @Override
    18     protected void onCreate(Bundle savedInstanceState) {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.activity_main);
    21     }
    22     @Override
    23     public boolean onCreateOptionsMenu(Menu menu) {
    24         //使用菜单填充器获取menu下的菜单资源文件
    25         getMenuInflater().inflate(R.menu.search_share_menu,menu);
    26         //获取搜索的菜单组件
    27         MenuItem menuItem = menu.findItem(R.id.search);
    28         searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    29         //设置搜索的事件
    30         searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    31             @Override
    32             public boolean onQueryTextSubmit(String query) {
    33               Toast t = Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT);
    34                 t.setGravity(Gravity.TOP,0,0);
    35                 t.show();
    36                 return false;
    37             }
    38             @Override
    39             public boolean onQueryTextChange(String newText) {
    40                 return false;
    41             }
    42         });
    43         //获取分享的菜单子组件
    44         MenuItem shareItem = menu.findItem(R.id.share);
    45         ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
    46         //通过setShareIntent调用getDefaultIntent()获取所有具有分享功能的App
    47         shareActionProvider.setShareIntent(getDefaultIntent());
    48         return super.onCreateOptionsMenu(menu);
    49     }
    50     //设置可以调用手机内所有可以分享图片的应用
    51     private Intent getDefaultIntent() {
    52         Intent intent = new Intent();
    53         intent.setAction(Intent.ACTION_SEND);
    54         //这里的类型可以按需求设置
    55         intent.setType('image/*');
    56         return intent;
    57     }
    58 }


    荣耀彩票代理 虽然用actionBar可以实现这些功能,但在各个安卓版本和定制Rom中的效果表现不一,导致严重的碎片化问题,所以Google在2015的IO大会上发布了系列的Material Design风格的控件。其中ToolBar是替代ActionBar的控件。后面我们也会简述ToolBar的简单用法。

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规