zoukankan      html  css  js  c++  java
  • 关于ActionBar的向下兼容

    1. Create a blank Android Project
    创建一个空的Android项目。
    a. 导入ActionBarCompat工程。
    ActionBarCompat的source code位置是:<Android SDK目录>/extras/android/support/v7/appcompat‘
    这样我们就得到一个名叫android-support-v7-appcompat 的library project
    b. 接着在自己新建的project点击右键->选择Properties->选择Android选项

    点Add, 然后选择 android-support-v7-appcompat

    3. Update Style Resources

    刚才说了ActionBarCompat在使用中会调用一些资源文件,尤其是基于Theme.AppCompat的主题(Theme)用来规范 Action Bar的显示。如果使用Action Bar的Activity没有使用基于Theme.AppCompat的主题,程序就不知道该如何配置Action Bar的显示,就会报错导致程序退出。

    在AndroidManifest中讲Application的 android:theme属性设置为Theme.AppCompat系列Theme。

    <application
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/Theme.AppCompat.Light">

    如果你在使用自定义的Theme,则该Theme的parent应设置为Theme.AppCompat系列Theme.

    <!-- Application theme. -->
    <style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/global_main_bg</item>
    </style>

    4. Extend ActionBarActivity
    当要在Activity中使用ActionBar,并要求兼容Android 2.1~3.0之间的系统时,我们不能像往常那样extend Activity,而应extend ActionBarActivity(原因如上所属,Android 3.0以前的系统中Activity API里是没有ActionBar接口的 自然也就无法调用。为了向下兼容,必须使用ActionBarActivity)。

    public class MainActivity extends ActionBarActivity {
    
    private ActionBar actionBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar=getSupportActionBar();
    //actionBar operation
    actionBar.setTitle("ActionBar");
    //....
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }
    }

    5. 修改menu/main.xml (也就是Action Bar中的Action Items)
    你的project中会有一个默认的main.xml,为了向Action Bar中添加几个功能按钮(也就是Action Items),我们需要对menu/main.xml进行些修改:

    在root element中添加一个attribute

    添加新的item项

    如下:

    <menu
    Action Items广泛使用的一些icon,你可以从Download the Action Bar Icon Pack下载到。xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
    android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="always" />
    <item
    android:id="@+id/action_search"
    android:title="@string/action_search"
    android:orderInCategory="1"
    android:icon="@drawable/action_search"
    app:showAsAction="always" />
    </menu>
    Action Items广泛使用的一些icon,你可以从Download the Action Bar Icon Pack下载到。
  • 相关阅读:
    Analysis Services features supported by SQL Server editions
    Azure DevOps to Azure AppServices
    Power BI For Competition
    Win10开机“提示语音”以及”随机播放音乐”
    Azure DevOps
    Allow Only Ajax Requests For An Action In ASP.NET Core
    Mobile CI/CD 101
    Configure SSL for SharePoint 2013
    AWS Step Function Serverless Applications
    Cordova Upload Images using File Transfer Plugin and .Net core WebAPI
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4551670.html
Copyright © 2011-2022 走看看