更新时间:2017-07-02 来源:黑马程序员Android+物联网培训 浏览量:
1.
在AndroidManifest.xml中,修改应用程序的主题,改为Sherlock下的某种主题,比如:@style/Theme.Sherlock.Light
,表示亮色风格的主题。
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" >
<activity
android:name="com.example.actionbardemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
public class MainActivity extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_1"
android:showAsAction="always"
android:icon="@drawable/ic_launcher"
android:title="action_1"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_1:
Log.d("Test", "action 1 is clicked!");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_1"
android:icon="@drawable/ic_launcher"
android:showAsAction="always"
android:title="action_1"/>
<item
android:id="@+id/action_more"
android:icon="@drawable/ic_launcher"
android:showAsAction="always"
android:title="action_more">
<menu>
<item
android:id="@+id/action_2"
android:showAsAction="always"
android:title="action_2"/>
<item
android:id="@+id/action_3"
android:showAsAction="always"
android:title="action_3"/>
</menu>
</item>
</menu>
private void updateActionBar() {
ActionBar actionbar = getSupportActionBar();//获取actionbar对象
actionbar.setTitle("自定义标题");//修改actionbar标题
actionbar.setLogo(R.drawable.logo);//修改actionbar的logo
actionbar.setDisplayHomeAsUpEnabled(true);//显示返回按键
}