滑动抽屉,处理抽屉的位置和宽度。

时间:2013-02-07 06:15:37

标签: android android-xml android-sliding

我最近偶然发现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>  

1 个答案:

答案 0 :(得分:1)

我找到了答案: 在我的xml中的幻灯片定义中:

    android:topOffset="100.5dp"