Android - 如何将SlidingUpPanelLayout设置为屏幕高度的一半?

时间:2016-11-30 11:56:52

标签: android-layout

我正在使用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只占用屏幕的一半吗?

1 个答案:

答案 0 :(得分: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.   
  3. 使用 ANCHORED 作为 PanelState
  4.   
slidingUpPanelLayout.setAnchorPoint(0.5f);
slidingUpPanelLayout.setPanelState(SlidingUpPanelLayout.PanelState.ANCHORED);

希望它有所帮助:)

相关问题