4

我正在尝试制作一个拆分菜单(有或没有夏洛克),但我仍然没有成功。

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 个没有标题和默认背景的小图标,我不知道如何更改。这就是我想要得到的:

拆分菜单底部

如何在不影响顶部菜单的情况下添加自定义底部菜单?

这是我发现的:

从清单中实现拆分菜单(我认为这不会让我有机会改变我想要的任何东西)

使用布局参数- 看起来它不能正常工作。


实现此菜单的最佳方法是什么?


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

4

1 回答 1

1

2个选项:

1)您可以只在清单中设置标志(或像上面那样以编程方式)来拆分ActionBar,但这仅适用于您在手机大小的纵向设备上,即使这样它仍然有ActionBar现在的顶部,但是将前几个ActionItems 移到底部。这在大多数情况下都很好,但听起来你是在说你总是希望这个底部显示,而你将使用的系统菜单无法按照你想要的程度设置样式。

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的 's,您可以在运行时确定所有这些onPrepareOptionsMenu(),并动态更改上面布局的版本。那将是更多的工作,但可能是您正在寻找的更多。

于 2013-10-02T19:36:06.990 回答