【问题标题】:How to have the same SlidingUpPanel for all activities如何为所有活动使用相同的 SlidingUpPanel
【发布时间】:2017-01-24 09:52:11
【问题描述】:
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.sothree.slidinguppanel.SlidingUpPanelLayout
        xmlns:sothree="http://schemas.android.com/apk/res-auto"
        android:id="@+id/sliding_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        sothree:umanoPanelHeight="68dp"
        sothree:umanoShadowHeight="4dp"
        sothree:umanoParallaxOffset="100dp"
        sothree:umanoDragView="@+id/dragView"
        sothree:umanoOverlay="true"
        sothree:umanoScrollableView="@+id/main">

        <!-- MAIN CONTENT -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.v7.widget.Toolbar
                xmlns:sothree="http://schemas.android.com/apk/res-auto"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/main_toolbar"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                sothree:theme="@style/ActionBar"
                android:layout_width="match_parent"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:layout_marginTop="?attr/actionBarSize"


            />
        </RelativeLayout>
        <!-- SLIDING LAYOUT -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:orientation="vertical"
            android:clickable="true"
            android:focusable="false"
            android:id="@+id/dragView">
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="68dp"
            android:orientation="horizontal"
            android:id="@+id/panel">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="68dp"
                android:orientation="vertical"
                android:layout_weight="0.8">

                <TextView
                    android:id="@+id/footer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:gravity="center_vertical"
                    android:paddingLeft="10dp"/>
                <TextView
                    android:id="@+id/foooter"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="16sp"
                    android:gravity="center_vertical"
                    android:paddingLeft="10dp"/>

            </LinearLayout>
            <ImageButton
                android:layout_width="68dp"
                android:layout_height="68dp"
                android:src="@mipmap/playy"
                android:id="@+id/play"
                android:background="@null"
                />
            <ImageButton
                android:layout_width="68dp"
                android:layout_height="68dp"
                android:src="@mipmap/queue"
                android:id="@+id/queue"
                android:background="@null"
                />
        </LinearLayout>
            <FrameLayout
                android:id="@+id/ss"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="#ffffff"
                >

              <!--  <TextView
                    android:id="@+id/list_main"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="?attr/actionBarSize"
                    android:gravity="center"
                    android:text="Main Content"
                    android:clickable="true"
                    android:focusable="false"
                    android:focusableInTouchMode="true"
                    android:textSize="16sp" />-->

                    <LinearLayout android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:layout_below="@+id/panel">

                        <LinearLayout
                            android:id="@+id/songThumbnail"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:paddingTop="10dp"
                            android:paddingBottom="10dp"
                            android:gravity="center"
                            android:layout_weight="0.9"
                            android:layout_marginTop="?attr/actionBarSize"
                           >

                            <ImageView android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:id="@+id/album_art"
                                android:src="@mipmap/music"
                                />
                        </LinearLayout>


                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical"
                            android:layout_above="@+id/linearLayout"
                            android:layout_alignParentStart="true"
                            >

                            <SeekBar
                                android:id="@+id/songProgressBar"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="20dp"
                                android:layout_marginLeft="20dp"
                                android:layout_marginBottom="20dp"

                                android:thumb="@mipmap/seek_handler"
                                                      android:progressDrawable="@mipmap/img_seekbar_bg"
                                android:paddingLeft="6dp"
                                android:paddingRight="6dp"/>

                            <!-- Timer Display -->
                            <LinearLayout
                                android:id="@+id/timerDisplay"
                                android:layout_above="@id/songProgressBar"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="20dp"
                                android:layout_marginLeft="20dp"
                                android:layout_marginBottom="10dp">
                                <!-- Current Duration Label -->
                                <TextView
                                    android:id="@+id/songCurrentDurationLabel"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1"
                                    android:gravity="left"
                                    android:textColor="#eeeeee"
                                    android:textStyle="bold"/>
                                <!-- Total Duration Label -->
                                <TextView
                                    android:id="@+id/songTotalDurationLabel"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1"
                                    android:gravity="right"
                                    android:textColor="#04cbde"
                                    android:textStyle="bold"/>


                            </LinearLayout>
                        </LinearLayout>
                        <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="200dp"
                            android:orientation="horizontal"
                            android:gravity="center"
                            android:layout_weight="0.1"
                            android:id="@+id/linearLayout"
                            >


                            <!-- Previous Button -->
                            <ImageButton
                                android:id="@+id/bt_previous"
                                android:src="@mipmap/previous"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@null"
                                android:layout_weight="2"
                                />

                            <!-- Backward Button -->

                            <!-- Play Button -->
                            <ImageButton
                                android:id="@+id/bt_play"
                                android:src="@mipmap/playy"
                                android:layout_width="100px"
                                android:layout_height="100px"
                                android:background="@null"
                                android:layout_weight="2"/>
                            <!-- Forward Button -->

                            <!-- Next Button -->
                            <ImageButton
                                android:id="@+id/bt_next"
                                android:src="@mipmap/next"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@null"
                                android:layout_weight="2"
                                />


                        </LinearLayout>
                    </LinearLayout>

            </FrameLayout>



        </LinearLayout>
    </com.sothree.slidinguppanel.SlidingUpPanelLayout>






</RelativeLayout>

上面是我的SlidingUpPanel的XML代码,在我的Main Activity中使用。它有两部分,主要内容(正常屏幕)和滑动内容(将从屏幕底部出现)。现在问题这个面板是否只会显示在主活动中。我想为我的所有活动显示面板。指导我如何去做。

请帮忙 谢谢..!!

【问题讨论】:

  • 将滑动面板放在 Fragment 中,并在需要时在 Activity 中添加 Fragment (developer.android.com/training/basics/fragments/index.html)
  • 我建议你将整个 SlidingPanel xml 放在一个布局文件中,并在每个活动中调用它。当然,使用 Fragments 是很困难的,但更令人信服。

标签: android android-layout android-activity drawerlayout slidingdrawer


【解决方案1】:

在单独的布局中创建您的滑动内容并在您的任何 Activity 中对其进行充气

 LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);


        View inflatedLayout = inflater.inflate(R.layout.layout1, null, false);
        inflatedLayout.setLayoutParams(lp);

【讨论】:

    【解决方案2】:

    如果您希望在所有活动中使用相同内容的相同滑动菜单,那么只需创建一个活动并编写滑动菜单的逻辑代码,将其名称为BaseActivity,然后在您项目的所有其他活动中扩展此BaseActivity而不是AppCompatActivity,因此您可以在所有活动中使用它。

    另一种方法是 Fragment 包含 @Veeresh 在 cmets 中建议的 SlidingMenu,这也是一个不错的选择

    像这样回答您的评论在您的 MainActivity 中创建一个变量

     public  SlidingUpPanelLayout ss; // this is important step , did you followed this in your mainactivity
    

    它是类变量,然后在 onCreate 方法中

    ss=(SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
    

    那么在其他活动中可以通过ss直接获取

    【讨论】:

    • 我是否需要为所有活动设置相同类型的 xml 布局,还是只需要一个 xml 布局来处理所有活动?
    • 不,只有一个布局包含滑动菜单,即 BaseActivity
    • MainActivity 扩展了 AppCompatActivity,SecondActivity 扩展了 MainActivity......这对吗?
    • 我已将我的滑动面板定义为 SlidingUpPanelLayout ss =(SlidingUpPanelLayout) findViewById(R.id.sliding_layout);在 MainActivity 中,是否需要在我的 SecondActivity 中创建 MainActivity 的对象才能访问slidinguppanel
    • 然后接受我的回答,点击我回答左上角upvote按钮下方的绿色复选标记
    猜你喜欢
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多