如何在代码中创建滑动抽屉?

时间:2010-05-14 04:55:21

标签: android set attributes slidingdrawer

我正在尝试在代码中创建一个滑动抽屉,但我不明白该怎么做构造函数的AttributeSet部分。

我需要做些什么?

另外,如何在代码中定义滑块显示的位置?

谢谢,

2 个答案:

答案 0 :(得分:7)

SlidingDrawer在Java代码中不能是新的,因为它必须定义句柄和内容,但你可以按如下方式在XML布局中膨胀:

sliding_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<SlidingDrawer
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:handle="@+id/handle"
    android:content="@+id/content">
    <ImageView
        android:id="@id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/tray_handle_bookmark"
        />
    <LinearLayout
        android:id="@id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#FF000000"
        />
</SlidingDrawer>

在Java代码中:

// you main Layout
LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        mainLayout.setOrientation(LinearLayout.VERTICAL);

// add sliding Drawer
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        slidingDrawer = (SlidingDrawer)inflater.inflate(R.layout.sliding_drawer, mainLayout, false);
        mainLayout.addView(slidingDrawer);

// get Layout for place your content in sliding drawer
LinearLayout slideContent = (LinearLayout)slidingDrawer.findViewById(R.id.content);

slideContent.addView(.....); // add your view to slideDrawer

答案 1 :(得分:2)

看起来无法在Java代码中直接创建SlidingDrawer。您需要在XML布局中定义它并使该布局膨胀。

抱歉!