【问题标题】:Android ActionBar - Push custom view to bottom of screenAndroid ActionBar - 将自定义视图推送到屏幕底部
【发布时间】:2012-08-05 11:31:33
【问题描述】:

我有一个拆分的 ActionBar,我正在尝试添加与 google play 的“正在播放”几乎相同的功能..

我可以使用 onCreateOptionsMenu 让菜单项出现在屏幕底部,但使用 actionBar.setCustomView 时我似乎无法让自定义视图出现在屏幕底部。自定义视图位于顶部 ActionBar 下方。

有谁知道如何将自定义视图强制到底部,或者将自定义视图添加到 onCreateOptionsMenu?

这是我的代码的 sn-ps:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    //Custom view to add
    actionBar.setCustomView(R.layout.albumitem);
            //This view shows up just under the top actionbar at the moment,
              despite menu items being at the bottom

菜单选项代码:(这些显示在我希望自定义视图所在的底部)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.option_menu, menu);

    return (super.onCreateOptionsMenu(menu));
}

我相信 google play 音乐应用底部的正在播放菜单是拆分操作栏中​​的自定义视图:

【问题讨论】:

标签: android android-actionbar


【解决方案1】:

所以我设法找到了一个肮脏的解决方案..好吧,在我的业余意见中很肮脏..

在 oncreateOptionsMenu 中,我添加了一个名为 option_menu 的菜单,如下所示:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.option_menu, menu);

在那个 option_menu 项的 xml 中,我实现了一个 actionViewClass,在这种情况下我使用了相对布局(我猜我可以只使用 View..?):

<item
    android:id="@+id/layout_item"
    android:actionViewClass="android.widget.RelativeLayout"
    android:showAsAction="always|withText"
    android:title="Text 1"/>

</menu>

然后我扩充了一个 xml 布局,并将其添加到我的菜单中定义的 layout_item 中:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.option_menu, menu);
        relativeLayout = (RelativeLayout) menu.findItem(R.id.layout_item)
                .getActionView();

        View inflatedView = getLayoutInflater().inflate(R.layout.now_playing,
                null);

        relativeLayout.addView(inflatedView);

        return (super.onCreateOptionsMenu(menu));
    }

请随时根据您的需要编辑/增强此答案。

【讨论】:

  • 它似乎可以工作,但我无法在顶部操作栏上显示图标,在底部栏上显示我的自定义视图。你也做到了吗?
  • 没有。我最近意识到使用底部的片段可能会更好。
  • 这个有点晚了,但音乐应用根本不使用操作栏。顶部和底部都是自定义视图。
  • aneal,我觉得tabs是actionbar的一部分,底视图是主要的activity布局,里面的内容是一个fragment。直到我发布这个问题后我才意识到这一点,但鉴于有一种方法可以将自定义视图膨胀到操作栏,我将问题和答案保持不变。这是一个解决方案,但不是最好的解决方案,特别是因为任何刷新选项菜单的调用都会导致膨胀的视图重新绘制,这可能会产生闪烁效果。
  • timusu 你有什么解决方案我必须实施同样请帮助我,如果你有任何解决方案
【解决方案2】:

我已经根据您的解决方案完成了一个解决方案,包括顶部自定义布局 + 底部自定义布局 + 标题/副标题 + 主页图标。代码如下:

public class SplitABActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar ab = getActionBar();
    ab.setTitle("Action Bar Title");
    ab.setSubtitle("Action Bar Subtitle");
    ab.setDisplayShowHomeEnabled(true);

    LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.ab_custom_layout, null);
    ab.setCustomView(layout);
    ab.setSplitBackgroundDrawable(new ColorDrawable(Color.BLUE));
    ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM  | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_including_layout, menu);
    RelativeLayout layout = (RelativeLayout) menu.findItem(R.id.layout_item).getActionView();
    View v = getLayoutInflater().inflate(R.layout.ab_bottom_layout, null);
    layout.addView(v); 

    return super.onCreateOptionsMenu(menu);
}
}

ab_custom_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    >
   <TextView android:id="@+id/element1" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"
       android:textColor="#fff"
       android:background="#ff0000"
       android:text="Element 1"/>

   <TextView
       android:id="@+id/element2" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textColor="#000"
        android:background="#f0f0f0"
        android:layout_toRightOf="@+id/element1"
       android:text="Element 2"/>

</RelativeLayout>

menu_including_layout.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/action1"
    android:title="Action 1"
      android:icon="@android:drawable/ic_dialog_alert"
      android:showAsAction="ifRoom" />

<item android:id="@+id/action2"
    android:title="Action 2"
      android:icon="@android:drawable/ic_dialog_info"
      android:showAsAction="ifRoom" />

<item
android:id="@+id/layout_item"
android:actionViewClass="android.widget.RelativeLayout"
android:showAsAction="always|withText"
android:title="Layout Item"/>
 </menu>

ab_bottom_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<TextView 
    android:id="@+id/bottom_layout_txt_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click this button"/>

<ImageButton android:id="@+id/bottom_layout_btn_chat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_menu_camera"
    android:layout_toRightOf="@+id/bottom_layout_txt_status"
    android:contentDescription="@string/app_name"/>
</RelativeLayout>

代码非常适合我。当我的平板电脑处于纵向时,我可以看到拆分的 ActionBar。在横向上,它的宽度足以在顶部存储所有项目。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多