【问题标题】:Android - how to set SlidingUpPanelLayout to half of the screen height?Android - 如何将 SlidingUpPanelLayout 设置为屏幕高度的一半?
【发布时间】:2016-11-30 11:56:52
【问题描述】:

我正在使用 Umano 的 SlidingUpPanelLayout 来提供一个可以从屏幕底部向上拖动的滑动面板。您可以在此处找到文档:

https://github.com/umano/AndroidSlidingUpPanel

我希望它在扩展时面板只占屏幕的一半。文档说明了这一点:

  • 主布局的宽度和高度应设置为 match_parent。
  • 滑动布局的宽度应设置为 match_parent,高度设置为 match_parent、wrap_content 或 max 理想的高度。如果您想将高度定义为 屏幕的百分比,将其设置为 match_parent 并定义一个 滑动视图的 layout_weight 属性。

这是我的包含滑动面板的 XML 布局:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.example.bleachedlizard.policeapp.mainactivity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.sothree.slidinguppanel.SlidingUpPanelLayout
            android:id="@+id/sliding_up_panel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom">

            <include layout="@layout/main_content" />

            <FrameLayout
                android:id="@+id/contact_details_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </FrameLayout>


        </com.sothree.slidinguppanel.SlidingUpPanelLayout>

    </FrameLayout>


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

我尝试将 layout_weight 属性添加到 contact_details_frame 但它不起作用。事实上,当我尝试添加 layout_weight 属性时,Android Studio 不会自动完成(我猜是因为它需要在 LinearLayout 中才能使该属性在那里有效)。我尝试添加一个 LinearLayout,既可以单独添加,也可以使用空视图来尝试占据屏幕的上半部分,但仍然没有。

谁能告诉我如何让 SlidingUpPanelLayout 只占据屏幕的一半?

【问题讨论】:

    标签: android-layout


    【解决方案1】:

    我测试了很多方法,但没有一种方法有效,但是这是另一种方法。希望对你有帮助:)

    首先:只需将 sothree:umanoPanelHeight 值更改为 0dp 即可隐藏 SlidingUpPanelLayout 的底栏

    <com.sothree.slidinguppanel.SlidingUpPanelLayout
            // your code
            sothree:umanoPanelHeight="0dp">
    

    第二个:制作barlayout按钮 [不在dragView中]
    打开 SlidingUpPanelLayout,而不是使用 SlidingUpPanelLayout 显示自己的栏

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_open_slide_panel"
        android:text="open SlidingUpPanelLayout "/>
    

    第三:在您的视图的 SetOnClickListener 方法中(我们在第二部分中制作)

    1. 调用方法 setAnchorPoint() 并传递 0.5f 作为参数
    2. 使用 ANCHORED 作为 PanelState
    slidingUpPanelLayout.setAnchorPoint(0.5f);
    slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.ANCHORED);
    

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      相关资源
      最近更新 更多