带有两个拉伸按钮的 Android 操作栏 [英] Android action bar with two stretched buttons

查看:23
本文介绍了带有两个拉伸按钮的 Android 操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何轻松实现带有两个拉伸按钮的操作栏吗?

Does anybody know how to easily implement an action bar with two stretched buttons?

以下是 Google 日历应用的示例:

Here is an example of the Google calendar app:

谢谢!

推荐答案

如果您出于某种原因希望在 ActionBar 中使用它,实现此目的的一种方法是在 Action Bar 上使用自定义视图.在您自定义视图的布局中,然后担心设置按钮宽度.

If you rather have this in the ActionBar for whatever reason, one way to achieve this is by using a custom view on the Action bar. Within you Custom View's layout then worry about setting up the buttons width.

告诉 Activity 为操作栏使用自定义视图:

Telling the activity to use a custom view for the action bar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   
    final ActionBar ab = getActionBar();
    ab.setDisplayShowHomeEnabled(false);
    ab.setDisplayShowTitleEnabled(false);     
    final LayoutInflater inflater = (LayoutInflater)getSystemService("layout_inflater");
    View view = inflater.inflate(R.layout.action_bar_edit_mode,null); 
    ab.setCustomView(view);
    ab.setDisplayShowCustomEnabled(true);
}

layout/action_bar_edit_mode.xml 看起来像这样:

layout/action_bar_edit_mode.xml can then look something like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="fill_horizontal"
    android:orientation="horizontal" >
    <LinearLayout 
        android:layout_alignParentLeft="true"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
        android:id="@+id/action_bar_button_cancel"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"           
        android:layout_weight="1"
        android:text="Cancel" />

        <Button
        android:id="@+id/action_bar_button_ok"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"           
        android:layout_weight="1"
        android:text="Ok" />
    </LinearLayout>    
</RelativeLayout>

希望对大家有所帮助!

注意:我意识到这里的嵌套布局很丑,通常我不推荐这样做,但出于某种原因,操作栏自己的布局拒绝让 LinearLayout 自己占据整个宽度.通常你应该避免像这样不必要地嵌套布局!也许如果有人看到这一点,他们可以为我们指出更好的解决方案?

Note: I realize the ugly nesting layouts here and normally I wouldn't recommend this but for some reason the actionbar's own layout refuses to let the LinearLayout take up the entire width on its own. Normally you should avoid nesting layouts unnecessarily like this! Perhaps if someone sees this they can point us to a better solution?

它的样子:

Roman 有一篇优秀帖子Nurik 在那里他很好地解释了一种方法.

There is an excellent post by Roman Nurik where he explains a way to do this very nicely.

编辑 2:如果有人好奇,那么布局按钮以便它们扩展操作栏的宽度而不需要像我上面那样嵌套布局的正确方法是设置具有适当布局参数的自定义视图,允许其组件与父组匹配.

EDIT 2: If anyone is curious, the correct way to lay out the buttons so that they expand the width of the actionbar without the need to nest your layouts like I did above, is to set the custom view with the proper layout parameters that allows its component to match the parent group.

本质上:

actionBar.setCustomView(view,new ActionBar.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT));

这篇关于带有两个拉伸按钮的 Android 操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆