在横屏模式下更改底部菜单的位置:「侧边菜单」。

8

这是在此处提出的问题的扩展:

如何实现从顶部打开 BottomSheet?

Android “Top Sheet” 与 “Bottom Sheet” 的等价物有哪些?

当然,“Bottom Sheet”的名字来源于其可以从底部滑动,因为它是被设计成如此的。

我发现在我的设计中,竖屏模式下它工作得非常好(图1和2),但是在横屏模式下,我想更好地利用“主区域”(图3)来获得更多的可见性。 因此,我想要一个“侧面板”,类似于导航抽屉(图4)。

enter image description here

到目前为止,我还没有找到解决方案或替代方法,所以欢迎任何帮助!


以下是演示横屏模式下问题的基本代码:

Main Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container_main"
    android:orientation="vertical">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <!-- Main Content -->
    <include layout="@layout/content_main"
        />

    <!-- Bottom Sheet Content -->
    <include layout="@layout/content_bottom_sheet"
        />

</android.support.design.widget.CoordinatorLayout>

</LinearLayout>

content_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.6">
    </LinearLayout>
</LinearLayout>

content_bottom_sheet.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:class="http://schemas.android.com/tools"
    android:id="@+id/bottomSheetLayout"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    app:behavior_peekHeight="50dp"
    android:orientation="vertical"
    app:layout_behavior="@string/bottom_sheet_behavior">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.9"
        android:weightSum="4">

        <ImageButton
            android:id="@+id/category0"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#BB45BBEC"
            android:onClick="jumpto0"
            />

        <ImageButton
            android:id="@+id/category1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAllCaps="false"
            android:layout_weight="1"
            android:background="#AAA"
            android:onClick="jumpto1"
            />

        <ImageButton
            android:id="@+id/category2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFEF53"
            android:onClick="jumpto2"
            />
        <ImageButton
            android:id="@+id/category3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FFF50E0E"
            android:onClick="jumptp3"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.2">

        <ImageButton
            android:id="@+id/bottom1"
            android:layout_width="98dp"
            android:layout_height="62dp"
            android:layout_gravity="center"
            android:background="#FFF50E0E"
            />

        <ImageButton
            android:id="@+id/bottom2"
            android:layout_width="98dp"
            android:layout_height="62dp"
            android:layout_gravity="center"
            android:background="#FFF5"
            />

    </LinearLayout>
</LinearLayout>
</LinearLayout>

或许你可以加上你已经尝试过的代码,因为这是一个编程问答网站。 - rmcsharry
1
示例代码已添加。 - Marios
有点晚了,但你成功获取想要的东西了吗,就像第4张图片中那样? - adriennoir
很遗憾,不行。我认为你可能需要更改CoordinatorLayout小部件的默认代码。 不过我仍然非常期待解决方案。 - Marios
1个回答

1
在这种情况下,你可以使用 RightSheetBehavior。但首先,你应该处理方向的变化,并根据方向替换行为。

非常抱歉回复晚了。 我的应用程序已经完成,无法测试,但它看起来很有前途。 如果我有机会,我会让你知道,但感谢你的答案,它可能仍然对其他人有帮助。 - Marios

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