2010-05-14 53 views
1

我想在代碼中創建滑動抽屜,但我不明白爲構造函數的AttributeSet部分做了什麼。如何在代碼中創建滑動抽屜?

我需要爲此做些什麼?

另外,如何在代碼中定義滑塊將要顯示的位置?

感謝,

+0

SlidingDrawer已經在Android中可用。即使如此,如果你想創建它,看看Android的實現。 – Karan 2010-05-14 06:06:08

+0

我知道,但我不明白如何使用這個構造函數 SlidingDrawer(Context context,AttributeSet attrs) 如何創建一個屬性集? – Alex 2010-05-14 09:31:00

回答

2

它看起來像SlidingDrawer不能在Java代碼中直接創建。您將需要在XML佈局中定義它並誇大該佈局。

對不起!

+0

好的,謝謝你的迴應 – Alex 2010-05-14 20:47:15

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