【问题标题】:ImageView onClick opens Sliding MenuImageView onClick 打开滑动菜单
【发布时间】:2014-03-10 10:58:31
【问题描述】:

所以我有一个应用程序,它使用一些按钮来进行不同的活动。在它们下方,我有图像按钮,我想使用其中的 2 个来打开 2 个不同的滑动菜单。我已经阅读了来自here 的所有关于滑动菜单的内容,但它只显示了使用滑动菜单的某种方式。

所以活动的代码是:

public class ChooseCategory extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_category);
}

我的 xml 代码是:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <LinearLayout    
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#BE2625"
    android:orientation="vertical">

    <Button
    android:id="@+id/fancy"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/fancy"
    android:textColor="#FFFFFF"
    android:background="@drawable/button4"
    />  

    <Button 
    android:id="@+id/chipchop"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/chipchop"
    android:textColor="#FFFFFF"
    android:background="@drawable/button4"
    />  

    <Button 
    android:id="@+id/pizza"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/pizza"
    android:textColor="#FFFFFF"
    android:background="@drawable/button4"
    />  

    <Button 
    android:id="@+id/asian"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/asian"
    android:textColor="#FFFFFF"
    android:background="@drawable/button4"
    />  

    <Button 
    android:id="@+id/indian"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/indian"
    android:textColor="#FFFFFF"
    android:background="@drawable/button4"
    />  

    <Button 
    android:id="@+id/kebab"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/kebab"
    android:textColor="#FFFFFF"
    android:background="@drawable/button4"
    />  

    <Button 
    android:id="@+id/other"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/other"
    android:textColor="#FFFFFF"
    android:background="@drawable/button4"
    />  
   </LinearLayout>

     <LinearLayout
        android:id="@+id/ll_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="bottom"
        android:background="#BE2620" >



                <!-- contentDescription tags added to remove related warnings -->

            <ImageButton
                android:id="@+id/btn_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:background="@null"
                android:src="@drawable/ic_launcher"
                 />

            <ImageButton
                android:id="@+id/btn_book"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:background="@null"
                android:src="@drawable/ic_launcher"
                  />

            <ImageButton
                android:id="@+id/btn_find_us"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:background="@null"
                android:src="@drawable/ic_launcher"
                  />

            <ImageButton
                android:id="@+id/btn_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:background="@null"
                android:src="@drawable/ic_launcher"
                  />


    </LinearLayout>
     </LinearLayout>
    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp"       
        android:background="#FFFFFF"/>


</RelativeLayouts>

关于如何使用该代码或其他代码来单击 ImageButton 并打开滑动菜单的任何帮助?

【问题讨论】:

  • 您想通过单击图像视图来打开滑动菜单。这是你想问的吗?
  • 如何从 ImageButton 打开滑动菜单。因为在上面的链接上,我无法理解如何将它用于 Imagebutton。
  • 操作栏上的图标。它被称为导航抽屉。

标签: android android-layout slidingmenu slidingdrawer


【解决方案1】:

我这样做是为了我的项目。

打电话

getSlidingMenu().toggle(); //to open left side menu

并调用

getSlidingMenu().showSecondaryMenu(); //to open right side menu

如果您想在单击 SherlockActionBar 中的图标后打开菜单

修改BaseActivity中的onOptionsItemSelected方法如下

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        toggle();
        return true;
    case R.id.github:
        //Util.goToGitHub(this);
        showSecondaryMenu();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

更新:

BaseActivity 扩展到您的班级

public class ChooseCategory extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_category);

        ImageButton button1 = (ImageButton) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                getSlidingMenu().toggle(); // to open left side menu
            }
        }

        ImageButton button2 = (ImageButton) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                getSlidingMenu().showSecondaryMenu(); // to open right side menu
            }
        }
    });
}

【讨论】:

  • 好的,但是这些方法 getSlidingMenu() 我应该在里面放什么?
  • 它无法识别 BaseActivity
  • 不仅 getSlidingMenu 无法识别。我应该让 SherlockActionBar 来做到这一点吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 1970-01-01
相关资源
最近更新 更多