有两个拉伸按钮的Andr​​oid行动起来吧 [英] Android action bar with two stretched buttons

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

问题描述

有谁知道如何轻松实现一个操作栏有两个拉伸按钮?

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

下面是谷歌日历应用程序的一个例子:

Here is an example of the Google calendar app:

感谢您!

推荐答案

如果你愿意有这样的动作条无论出于何种原因,实现这一目标的一个方法是使用操作栏上的自定义视图。在您自定义视图的布局则担心设置按钮的宽度。

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.

讲活动使用自定义视图的操作栏:

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);
}

布局/ 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>

希望它可以帮助别人!

Hope it helps someone!

注意:我知道丑陋的嵌套布局在这里,通常我不会推荐这一点,但由于某些原因,动作条本身的布局拒绝让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?

是什么样子:

What it looks like:

编辑:有由罗马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));

这篇关于有两个拉伸按钮的Andr​​oid行动起来吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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