Android设置按钮最右边,将ActionBar主页按钮设置到右侧

您可以创建这样的操作栏,但它比膨胀菜单稍微复杂一些。在onCreateOptionsMenu()方法中创建的菜单将始终对齐到右侧,放置在拆分操作栏中​​或隐藏在菜单键下方。

如果您希望您的操作栏只包含两个菜单项 - 一个在左边,另一个在右边 - 您必须在操作栏中创建自定义视图。

自定义视图布局将是这样的:

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:background="?attr/actionBarItemBackground"

android:layout_width="?attr/actionBarSize"

android:layout_height="match_parent"

android:scaleType="centerInside"

android:id="@+id/item1"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"/>

android:background="?attr/actionBarItemBackground"

android:layout_width="?attr/actionBarSize"

android:layout_height="match_parent"

android:scaleType="centerInside"

android:id="@+id/item2"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"/>

定义中要使用的自定义视图的主题。主题应包括:

@style/MyActionBarStyle

@style/MyActionBarStyle

showCustom

showCustom

设置在活动(或片段)的自定义视图:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

ActionBar ab = getSherlock().getActionBar();

LayoutInflater li = LayoutInflater.from(this);

View customView = li.inflate(R.layout.my_custom_view, null);

ab.setCustomView(customView);

ImageButton ibItem1 = (ImageButton) customView.findViewById(R.id.item1);

ibItem1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

// ...

}

});

ImageButton ibItem2 = (ImageButton) customView.findViewById(R.id.item2);

ibItem2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

// ...

}

});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值