如何在Android中制作自定义的底部拆分菜单

4

我正在尝试制作一个分割菜单(使用sherlock或不使用),但我仍未成功。

public class MainActivity extends SherlockFragmentActivity  {
ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);
...............
    mSherlock.setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);
    mSherlock.setContentView(R.layout.activity_main);
...............
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Used to put dark icons on light action bar
    boolean isLight = false;

    menu.add("You")
        .setTitle("You")
        .setIcon(isLight ? R.drawable.icon_you : R.drawable.icon_you)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    menu.add("Pet Open")
        .setIcon(isLight ? R.drawable.icon_pet_open : R.drawable.icon_pet_open)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    menu.add("Around")
        .setIcon(isLight ? R.drawable.icon_around : R.drawable.icon_around)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return true;
}

作为结果,我得到了3个没有标题且具有默认背景的小图标,我不知道如何更改。这是我想要获得的内容:

Split Menu Bottom

我该如何添加自定义底部菜单,而不影响顶部菜单?
以下是我找到的内容: 从清单中实现拆分菜单(我认为这不会给我改变任何想要的机会) 使用布局参数 - 看起来它不能正常工作。

如何最好地实现这个菜单?


更新 这里是我发现的另一种方法: 好处是我可以看到如何更改它,但坏处是它在顶部和底部使用相同的背景,我不确定是否可以更改。 输入图像描述


1
我认为你添加的示例与原始代码并没有太大区别。他使用ActionItem就像通常一样,只是将ActionBar设置为覆盖屏幕,使其半透明。我在代码中没有看到任何更容易让你样式化菜单的东西。 - Jon
1个回答

1

两个选项:

1)你可以在清单中设置标志(或像上面一样以编程方式设置)来分割ActionBar,但这仅适用于手机大小的设备在纵向模式下,并且即使如此,它仍然在顶部显示ActionBar,但将前几个ActionItem移动到底部。对于大多数情况来说,这是可以接受的,但听起来你是说你总是想要这个底部显示,并且你将使用的系统菜单没有你想要的样式。

2)为屏幕底部创建自己的布局,以创建你所说的样式化菜单。这可能会起作用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <!-- this is where the Activity contents go -->
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:background="#8f8"
    android:orientation="horizontal" >

    <!-- this is the green bottom bar -->
</LinearLayout>

<LinearLayout
    android:layout_width="48dp"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentleft="true"
    android:layout_margin="5dp"
    android:orientation="vertical" >

    <!-- This is the status button -->

    <ImageView
        android:layout_width="48dp"
        android:layout_height="40dp"
        android:background="#00f" />

    <TextView
        android:layout_width="48dp"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:text="Status"
        android:textAlignment="center"
        android:textSize="12sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="60dp"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="5dp"
    android:orientation="vertical" >

     <!-- This is the Pet Open button -->


    <ImageView
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:background="#00f" />

    <TextView
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:text="Pet Open"
        android:textAlignment="center"
        android:textSize="12sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="60dp"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_margin="5dp"
    android:orientation="vertical" >


     <!-- This is the Refresh button -->

    <ImageView
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:background="#00f" />

    <TextView
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:text="Refresh"
        android:textAlignment="center"
        android:textSize="12sp" />
</LinearLayout>

</RelativeLayout>

这将适用于3个按钮,需要进行一些调整以使其更接近您想要的样式。如果您希望在底部显示超过3个按钮,并且想要使用提供的MenuItem,则可以在{{link1:onPrepareOptionsMenu()}}中在运行时确定所有内容,并动态更改上面布局的版本。这将需要更多的工作,但可能更符合您的需求。

谢谢。我现在会尝试一下。请检查更新后的问题,可能还有另一种方法。 - Filip Luchianenco
谢谢,我想我会选择第一种方式,因为在更大的屏幕上,底部栏看起来很糟糕,而且在横向模式下,按钮会放在顶部,这是最好的选择,因为空间较小,所以在横向模式下,3个按钮的高度会太高。 - Filip Luchianenco
那似乎是最好的选择。一般来说,ActionBar 的工作效果很好,并且具有足够的内置功能,使得自己制作定制解决方案不值得。 - Jon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接