安卓选项菜单 - 单行中有多个项目



我想创建一个选项菜单,总共有 5 个项目。 但是,我希望将两个项目并排存在于一行中,而其他 3 个项目可以单独存在于自己的单独行中。

使用..... 添加单独存在于其自己行上的项目是微不足道的.....

但是,问题是如何让 2 并排存在于一行上?

不幸的是,真正的选项菜单(由 onCreateOptionsMenu(Menu menu) 创建)需要menu资源,该资源只是具有item节点的 XML。如果您使用DialogPopupWindow创建自定义菜单,那么您实际上可以做任何您想做的事情。在这种情况下,请创建一个具有垂直LinearLayout的布局,其中包含一些水平LinearLayout,其中包含并排元素和常规元素。

<?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:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</LinearLayout>

对存在于其自己行上的项目使用垂直线性布局,对必须并排存在的两个项目使用水平线性布局。

在 xml 中,LinearLayout 属性是android:orientation="vertical""horizontal",具体取决于您使用的属性。

最新更新