【问题标题】:Android sliding drawer from top to bottomAndroid从上到下滑动抽屉
【发布时间】:2017-01-08 15:42:43
【问题描述】:

我想在 android 中创建滑动抽屉,它应该放在 Activity 的顶部,并且应该像 android 通知面板一样从上到下打开。如何做到这一点?

【问题讨论】:

标签: android slidingdrawer


【解决方案1】:

默认的SlidingDrawer 类不允许这样做。您可以使用此处的 Panel 类来获得非常相似的内容,或者为SlidingDrawer、内容和句柄设置 180º 的旋转。 注意:android:rotation 支持 API > Level 11

http://www.ohloh.net/p/android-misc-widgets
https://github.com/IanDarwin/Android-Cookbook-Examples/tree/master/SlidingDrawer-Topdown

Reference

【讨论】:

  • 我无法运行食谱示例,它给我的错误为 03-02 16:44:06.023: E/AndroidRuntime(21584): java.lang.RuntimeException: Unable to start activity ComponentInfo{com .example.slidingdrawer/com.example.slidingdrawer.MainActivity}: java.lang.RuntimeException: 你的面板必须有一个视图,其 id 属性是'R.id.panelHandle'
【解决方案2】:

如果你想从上到下滑动抽屉,那么只需将这段代码复制粘贴到你的项目中,它是 100% 工作代码。

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:background="#d3d3d3"
    >
    <SlidingDrawer
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/slidingDrawer"
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:content="@+id/content"
    android:gravity="center_horizontal"
    android:handle="@+id/handle"
    android:orientation="vertical"
    android:rotation="180" >

    <LinearLayout
        android:id="@+id/handle"
        android:layout_width="fill_parent"
        android:layout_height="40dp" 
        android:gravity="right"
       android:background="@+drawable/drawer_bk"
           >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@+drawable/drawer1" />



    </LinearLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="45dp" >

    <RelativeLayout
        android:id="@+id/rel1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:rotation="180" >

        <ImageView
            android:id="@+id/ImageView04"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:onClick="visit"
            android:src="@drawable/visit_icon" />

        <ImageView
            android:id="@+id/ImageView03"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentTop="true"
            android:layout_marginRight="35dp"
            android:layout_toLeftOf="@+id/ImageView04"
            android:onClick="contact_us"
            android:src="@drawable/contact_icon" />

        <ImageView
            android:id="@+id/ImageView02"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="35dp"
            android:layout_toRightOf="@+id/ImageView04"
            android:onClick="logout"
            android:src="@drawable/singout_icon" />
    </RelativeLayout>


</RelativeLayout>

  </SlidingDrawer>

MainActivity.java

 public class MainActivity extends Activity 
 {

/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 4000;
Button slideButton,b1, b2,b3,b4;
SlidingDrawer slidingDrawer;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //slideButton = (Button) findViewById(R.id.slideButton);
    slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
     slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
        @Override
        public void onDrawerOpened()
        {
           // slideButton.setBackgroundResource(R.drawable.down_arrow_icon);
            slidingDrawer.setBackgroundResource(R.drawable.hd_img_1);
        }
    });

    slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() 
    {
        @Override
        public void onDrawerClosed() 
        {

          //  slideButton.setBackgroundResource(R.drawable.upwar_arrow_icon);
            slidingDrawer.setBackgroundColor(Color.TRANSPARENT);
        }
    });

   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多