2012-05-24 74 views
0

我对SlidingDrawer有问题。其中的所有按钮都无法点击。无法点击SlidingDrawer中的按钮

这里是我的SlidingDrawer xml文件:

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

    <Button 
     android:id="@+id/handle" 
     android:layout_width="wrap_content" 
     android:layout_height="30dp" 
     android:text="@string/menu" /> 

    <LinearLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="80dp" 
     android:layout_gravity="center" 
     > 

     <Button 
      android:id="@+id/btn_video" 
      style="@style/button_menu_bottom" 
      android:drawableTop="@drawable/mnu_video" 
      android:text="@string/video" /> 

     ... 
     ... 

    </LinearLayout> 

</SlidingDrawer> 

这里是我的java文件从SlidingDrawer

包com。示例延长;

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.SlidingDrawer; 
import com.example.R; 

public class BottomMenuBar extends SlidingDrawer { 

    private SlidingDrawer mSlidingDrawer; 
    private Button mButtonSlidingDrawer; 
    private LinearLayout mLayoutContent; 
    private Button mButtonVideo; 
    private Button mButtonPhoto; 
    private Button mButtonChat; 
    private Button mButtonSetting; 

    public BottomMenuBar2(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     String infService = Context.LAYOUT_INFLATER_SERVICE; 
     LayoutInflater li; 
     li = (LayoutInflater) getContext().getSystemService(infService); 
     li.inflate(R.layout.bar_bottom_menu, this, true); 
     mSlidingDrawer=(SlidingDrawer)findViewById(R.id.sld_bottom_menu); 
     mButtonSlidingDrawer=(Button)findViewById(R.id.handle); 
     mLayoutContent=(LinearLayout)findViewById(R.id.content); 
     mButtonVideo=(Button)findViewById(R.id.btn_video); 
     mButtonSetting=(Button)findViewById(R.id.btn_setting); 
    } 

    public Button getmButtonSlidingDrawer() { 
     return mButtonSlidingDrawer; 
    } 
    public void setmButtonSlidingDrawer(Button mButtonSlidingDrawer) { 
     this.mButtonSlidingDrawer = mButtonSlidingDrawer; 
    } 

    ... 
    Setter & Getter methods 
    ...  
} 

这里是我的main.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg_main" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.1" > 

     ... 
     Any content 
     ... 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <com.example.BottomMenuBar 
      android:id="@+id/sld_bottom_menu" 
      android:layout_width="wrap_content" 
      android:layout_height="110dp" 
      android:handle="@+id/handle" 
      android:content="@+id/content" 
      /> 

    </LinearLayout> 

</LinearLayout> 

回答

0

对于我的按钮滑动抽屉,我告诉每个按钮有什么方法在XML中使用与。

 <Button 
      android:id="@+id/sortbutton" 
      android:layout_width="0dp" 
      android:layout_height="35dp" 
      android:layout_weight="1" 
      android:onClick="SortClickHandler" 
      android:text="@string/sd_sortmethod_button" 
      android:textSize="12dp" /> 

,然后定义在任何活动是适当的方法:

public void SortClickHandler(View v) { 
    // Do stuff here 
} 

编辑

我想我找到了你的问题。从开发文档here它说,我引用:

SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer 
should only be used inside of a FrameLayout or a RelativeLayout for instance. 

所以,如果你改变你的布局为RelativeLayout的你会发现,你的按钮会工作,我想。我只是仔细检查了所有使用滑动抽屉的情况,并且在所有情况下,包含它的视图的根元素都是RelativeLayout。

+0

仍然无法点击。 –

+0

也许你的风格或其他方面正在干扰。尝试只是一个普通的旧按钮,摆脱风格和绘制,并插入宽度和高度,看看会发生什么... – Barak

+0

我想我找到你的问题。更新我的答案,检查出来。 – Barak

0

我不明白你为什么要延长SlidingDrawer

你应该查阅文档here

<SlidingDrawer 
android:id="@+id/drawer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 

android:handle="@+id/handle" 
android:content="@+id/content"> 

<ImageView 
    android:id="@id/handle" 
    android:layout_width="88dip" 
    android:layout_height="44dip" /> 

<FrameView 
    android:id="@id/content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    [INSERT VIEWS HERE] 
<FrameView> 

</SlidingDrawer> 

在你Activity,你甚至不技术上需要访问到SlidingDrawer对象(滑动功能已经到位为标准,它只包含在content的视图的,你不需要干扰)。

从您的Activity获得对您添加的视图的引用[INSERT VIEWS HERE]。然后,您可以在每个按钮上执行setOnClickListener()