【问题标题】:Android action bar with two stretched buttons带有两个拉伸按钮的 Android 操作栏
【发布时间】:2012-06-29 15:40:19
【问题描述】:

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

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

谢谢!

【问题讨论】:

标签: android button android-actionbar stretched


【解决方案1】:

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

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

@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 看起来像这样:

<?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>

希望它对某人有所帮助!

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

它的样子:

编辑: Roman Nurik 有一个excellent post,他在其中解释了一种非常好的方法。

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

基本上:

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

【讨论】:

  • 以这种方式使用自定义视图(具有拉伸的相对布局)具有隐藏通常在某些设备上显示的溢出菜单的令人讨厌的副作用。
  • 并不是真正令人讨厌的副作用,这实际上是这种情况下所需的效果。如果您希望溢出仍然显示,您可能需要遵循联系人应用程序的操作(或 Roma Nurik 项目上的“DoneButtonActivity)并将按钮向左对齐以允许溢出仍然显示。您可以选择设置您的活动带有拆分操作栏,因此溢出显示在底部。
【解决方案2】:

我知道有 2 种方法可以做到这一点,但其中一种方法无法保持领先。

这是第一个:

您需要重写方法 onCreateOptionsMenu,但这是在 ActionBar 上添加的,您需要 API 11 来执行此操作,并且当您旋转屏幕时,此按钮会出现在 ActionBar 上,这取决于屏幕大小。

 @Override
 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
 {
     MenuItem add = menu.add(Menu.NONE, ADD_TIME, 0, R.string.add_time);
     add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

     MenuItem qkAdd = menu.add(Menu.NONE, QUICK_ADD_TIME, 1, R.string.quick_add_time);
     qkAdd.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
 }

结果如下:

如果您使用的是 Fragment,则需要将 setHasOptionsMenu 设置为 true,否则菜单不会显示。

这是第二个:

cancel_done.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_bar"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:dividerPadding="12dp"
    android:orientation="vertical"
    android:showDividers="end" >

    <Button
        android:id="@+id/button1"
        style="@drawable/btn_cab_done_holo_light"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="@drawable/btn_cab_done_holo_light"
        android:text="CANCEL"
        android:textSize="14sp" />

</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:dividerPadding="12dp"
    android:orientation="vertical"
    android:showDividers="beginning" >

    <Button
        android:id="@+id/button2"
        style="@drawable/btn_cab_done_holo_light"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="@drawable/btn_cab_done_holo_light"
        android:text="DONE"
        android:textSize="14sp" />

</LinearLayout>
</LinearLayout>

资源样式 btn_cab_done_holo_light.xml 您可以在 ..\sdk\platforms\android-%%\data\res\drawable 上找到,然后在您的布局中找到只需添加:

        <include
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_gravity="fill"
            layout="@layout/cancel_done" />

结果如下:

我现在不知道是否是最好的方法,但它正在工作。

【讨论】:

    【解决方案3】:

    用两个按钮制作一个水平线性布局。然后将它们的每个宽度设置为match_parentandroid:layout_weight="0.5" (然后每个按钮将占用 50% 的空间)。

    编辑:

    申请为ActionBar背景:

    (ActionBarSherlock) getSupportActionBar().setCustomView(R.layout.my_view);
    (ActionBar) getActionBar().setCustomView(R.layout.my_view);
    

    【讨论】:

    • 如何将这个LinearLayout应用到ActionBar?
    • 我的建议是不要使用 ActionBar;它涉及通过使用带有 2 个按钮和 layout_weight 的 LinearLayout 手动创建自己的“ActionBar”
    • 好吧,似乎没有更好的解决方案了。
    • 我对此进行了调整以向您展示如何设置实际的 ActionBar 布局(以及用于 Sherlock)
    【解决方案4】:

    您可以使用 ActionBar 的 actionMode 来实现 Done 和 Cancel 动作。

    http://developer.android.com/guide/topics/ui/menus.html#CAB

    【讨论】:

      【解决方案5】:

      在android中称为done bar。看看这个会有帮助的 https://github.com/googlesamples/android-DoneBar

      【讨论】:

        猜你喜欢
        • 2013-02-23
        • 1970-01-01
        • 2018-07-21
        • 1970-01-01
        • 2015-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多