Eri*_*ira 3 xml android android-layout android-activity
我正在构建和完成一个Android应用程序,这是我需要修复它的主要内容之一.
所以,我有一个动作栏样式如下:
<resources>
<style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBarTheme</item>
</style>
<style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#F6E6E7</item>
<item name="android:textSize">10sp</item>
</style>
<style name="CustomTabWidget">
<item name="android:textSize">15sp</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
这是我放置按钮的布局
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/voltar"
android:title="Voltar"
android:icon="@drawable/abc_ic_ab_back_holo_light"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
</menu>
Run Code Online (Sandbox Code Playgroud)
这个按钮标题Voltar位于动作栏的右侧..我该怎么做才能把它放在左侧?
谢谢你的阅读
你需要做两件事.首先在onCreate您的活动中声明这一点
getActionBar().setDisplayHomeAsUpEnabled(true);
Run Code Online (Sandbox Code Playgroud)
然后实现onOptionsItemSelected它,以便它作为向上按钮单击事件处理主页
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home ) {
finish();
return true;
}
// other menu select events may be present here
return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
getActionBar().setDisplayHomeAsUpEnabled(true);在onCreate中声明你的活动是正确的.
但是,Nayan在上面的帖子中建议的onOptionsItemSelected实现可以改进,并且应该按照以下代码处理:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
// finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
id android.R.id.home表示Home或Up按钮.声明getActionBar().setDisplayHomeAsUpEnabled(true);将导致显示向上(向后/向左箭头)按钮.使用NavUtils允许用户在应用程序结构中向上导航一个级别.
有关更多详细信息,请参阅Android设计上的导航模式:http: //developer.android.com/design/patterns/navigation.html#up-vs-back
| 归档时间: |
|
| 查看次数: |
7722 次 |
| 最近记录: |