I'm trying to create a sliding drawer in code, but I don't understand wh开发者_StackOverflow中文版at to do for the AttributeSet part of the constructor.
What do I need to do for that?
Also, how do I define in code where the slider is going to show up?
Thanks,
SlidingDrawer can't new in Java code because it must define handle and content but you can inflate in XML layout as follows:
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>
In Java code:
// 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
It looks like SlidingDrawer
cannot be created directly in Java code. You will need to define it in an XML layout and inflate that layout.
Sorry!
精彩评论