我最近偶然发现了 Android 中的滑动抽屉,在创建了我的第一个实现相同功能的项目后,我遇到了一些问题。我在滑动抽屉内使用列表视图,为此我需要实现 onClickListener 并扩展 Activity,现在一切正常,尽管滑动抽屉打开并占据了视图中的几乎所有空间,我希望它打开只有半屏。我浏览了互联网上的各种帖子,要求我扩展 SlidingDrawer 并使用 onMeasure 方法,这对我来说是不可行的(或者是吗?)。其次,我想将手柄按钮放在顶部而不是中心。下面是我的代码:
activity_main.xml~~~>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="top"
>
<TextView
android:text=""
android:gravity="center|center_vertical"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<SlidingDrawer
android:layout_width="wrap_content"
android:id="@+id/SlidingDrawer"
android:handle="@+id/slideButton"
android:content="@+id/contentLayout"
android:animateOnClick="true"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/slideButton"
>
</Button>
<LinearLayout
android:layout_width="wrap_content"
android:id="@+id/contentLayout"
android:orientation="vertical"
android:gravity="center"
android:padding="10dip"
android:background="#0080FF"
android:layout_height="wrap_content">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
.java file~~~~>
public class slidingDrawerDemo extends ListActivity implements OnClickListener {
private ArrayAdapter<String> listAdapter ;
Button slideButton,b1, b2,b3,b4;
SlidingDrawer slidingDrawer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slideButton = (Button) findViewById(R.id.slideButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
ListView LV = getListView(); //(ListView)findViewById(R.id.list);
String []Test ={"Residential","Commercial","Customised Projects","SEZ","Brochure"};
ArrayList<String> Plist = new ArrayList<String>();
Plist.addAll(Arrays.asList(Test));
listAdapter = new ArrayAdapter<String>(this, R.layout.list_dem, Plist);
LV.setAdapter( listAdapter );
LV.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView LV, View view,int position, long id) {
Toast.makeText(slidingDrawerDemo.this,""+position, Toast.LENGTH_SHORT).show();
if(position==0){
initOne();
}else if (position==1){
initTwo();
}else if (position==2){
initThree();
}else if (position==3){
initFour();
}else if (position==4){
initFive();
}
}
});
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
// slideButton.setBackgroundResource(R.drawable.closearrow);
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
// slideButton.setBackgroundResource(R.drawable.openarrow);
}
});
}
My list_dem.xml file~~~>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>