2013.12.03(2) ——— android ActionbarSherlockSample之Simple

本文详细介绍了一个用于Android平台的开源项目——ActionBarSherlock的使用方法。该项目旨在为不同版本的Android系统提供一致的ActionBar体验。文章提供了如何配置主题、显示返回按钮、自定义视图等实用技巧,并介绍了如何调整菜单键行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2013.12.03(2) ——— android ActionbarSherlockSample之Simple

ActionbarSherlockSample,简称abs,是一个主要用来兼容ActionBar的开源项目
[url]https://github.com/JakeWharton/ActionBarSherlock[/url]


/*
* Copyright (C) 2011 Jake Wharton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.actionbarsherlock.sample.demos;

import android.os.Bundle;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;

public class Simple extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(SampleList.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
((TextView)findViewById(R.id.text)).setText(R.string.simple_content);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}



只有一点:android.R.id.home就是点了左上角的<符号,那如何控制<符号的显示呢?

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//getActionBar().setDisplayHomeAsUpEnabled(true);//这个是android自己的设置方法



一些常见api:


1、是否显示actionbar
getSupportActionBar().setDisplayShowTitleEnabled(true)

2、是否显示自定义的view
getSupportActionBar().setDisplayShowCustomEnabled(true);
一般和
getSupportActionBar().setCustomView(R.layout.custom_view);

3、自定义logo
<item name="android:icon">@drawable/action_bar_title_maintab_logo</item>
<item name="icon">@drawable/action_bar_title_maintab_logo</item>


4、修改右上角更多按钮下拉框的样式
背景色
<style name="CustomMainPopupMenu" parent="android:Widget.Holo.PopupMenu">
<item name="android:popupBackground">#ffffff</item>
</style>

间隔线的高度和颜色
<style name="CustomMainDropDown" parent="Sherlock.__Widget.Holo.ListView">
<item name="android:dividerHeight">1px</item>
<item name="android:divider">@color/tab_hot_popumenu_line</item>
</style>

字体
<style name="CustomMainPopupMenuTextLarge" parent="TextAppearance.Sherlock.Widget.PopupMenu.Large">
<item name="android:textColor">#546f8a</item>
</style>

<style name="CustomMainPopupMenuTextSmall" parent="TextAppearance.Sherlock.Widget.PopupMenu.Small">
<item name="android:textColor">#546f8a</item>
</style>


使用
<style name="MainActivityTheme" parent="Theme.Sherlock">
<item name="popupMenuStyle">@style/CustomMainPopupMenu</item>
<item name="android:popupMenuStyle">@style/CustomMainPopupMenu</item>
<item name="dropDownListViewStyle">@style/CustomMainDropDown</item>
<item name="android:dropDownListViewStyle">@style/CustomMainDropDown</item>
<item name="textAppearanceLargePopupMenu">@style/CustomMainPopupMenuTextLarge</item>
<item name="android:textAppearanceLargePopupMenu">@style/CustomMainPopupMenuTextLarge</item>
<item name="textAppearanceSmallPopupMenu">@style/CustomMainPopupMenuTextSmall</item>
<item name="android:textAppearanceSmallPopupMenu">@style/CustomMainPopupMenuTextSmall</item>
</style>



5、屏蔽menu实体键

//忽略实体menu键
private void ignoreMenu(){
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}


在Application的onCreate里面 调用 这个 就可以屏蔽menu键,让overflow常显

但是 小米v4 没有成功
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值