如何在LibSlideMenu中设置图像?

时间:2017-02-14 06:25:23

标签: android image android-activity imageview fragment

我正在使用LibSlideMenu库作为滑块。在那我想要设置图像。 我的活动包含

private void attachSlideMenu() 
{   
slidemenu = (SlideMenu) findViewById(R.id.slideMenu);
slidemenu.init(this, R.menu.myslide, new SlideMenuListener(
        (Activity) this,R.id.item_home), 333);  
}
public void clickEvent(View v) 
{
if (v.getId() == R.id.button1) 
{
slidemenu.show();
}
}

我的SlideMenuListener是

public class SlideMenuListener implements OnSlideMenuItemClickListener {
Activity activity;
int sidemenu_id;
static boolean isConnected = false;
ProgressBar side_progress_bar;

public SlideMenuListener(Activity activity,int sidemenu_id) {
    super();
    this.activity = activity;
    this.sidemenu_id=sidemenu_id;
    ((SlideMenu)activity.findViewById(R.id.slideMenu)).setHeaderImage(activity.getResources().getDrawable(
            R.drawable.ic_launcher));       
}

@Override
public void onSlideMenuItemClick(int itemId) {

    switch (itemId) {

        case R.id.item_home:    
             //do something
             break;
        case R.id.item_ten: 
             //do something
            break;              
        case R.id.item_all: 
             //do something 
            break;  
        case R.id.item_eight:   
             //do something 
            break;  
        case R.id.item_nine:    
             //do something
            break;  
        case R.id.item_six: 
             //do something
            break;  
        case R.id.item_five:    
             //do something
            break;             

        }
      }
    }

myslide.xml菜单是

<?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
     android:background="@color/black">
    <item
        android:id="@+id/item_home"
        android:icon="@drawable/book_ride_menu"
        android:title="@string/item_home"
        android:titleCondensed="@string/item_dhome">
   </item>
   <item
    android:id="@+id/item_ten"
    android:icon="@drawable/riky_wifi_menu"
    android:title="@string/item1"
    android:titleCondensed="@string/item_dten">
   </item>  
   <item
    android:id="@+id/item_all"
    android:icon="@drawable/menu"
    android:title="@string/item2"
    android:titleCondensed="@string/item_dten">
  </item> 
  <item
    android:id="@+id/item_eight"
    android:icon="@drawable/entertainment"
    android:title="@string/item3"
    android:titleCondensed="@string/item_deight">
  </item>      
  <item
    android:id="@+id/item_nine"
    android:icon="@drawable/myvideos"
    android:title="@string/item4"
    android:titleCondensed="@string/item_deight">
</item>        
 <item
    android:id="@+id/item_six"
    android:icon="@drawable/signout"
    android:title="@string/item6"
    android:titleCondensed="@string/item_dsix">
</item> 
  <item
    android:id="@+id/item_five"
    android:icon="@drawable/signout"
    android:title="@string/item7"
    android:titleCondensed="@string/item_dfive">
</item>   
</menu>

我想要这样的东西,如图所示:

Image

一切正常。但我不知道如何设置pic图像视图。

1 个答案:

答案 0 :(得分:0)

我有另外一种方法来解决这个问题。我从我的项目中删除了LibSlideMenu库并添加了自定义滑块,如下所示: MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
 @Override
   protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);  
    // Initialize
    metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    panelWidth1 = (int) ((metrics.widthPixels) * 0.75);
    headerPanel = (RelativeLayout) findViewById(R.id.header);
    headerPanelParameters = (LinearLayout.LayoutParams) headerPanel
            .getLayoutParams();
    headerPanelParameters.width = metrics.widthPixels;
    headerPanel.setLayoutParams(headerPanelParameters);

    slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
    slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel
            .getLayoutParams();
    slidingPanelParameters.width = metrics.widthPixels;
    slidingPanel.setLayoutParams(slidingPanelParameters);

    // Slide the Panel
    menuRightButton = (ImageView) findViewById(R.id.menuViewButton);
    menuRightButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            isExpanded();
        }
    });
  }
   public void isExpanded(){
  if (!isExpanded) {
        isExpanded = true;
        // Expand
        LeftMenuFragment leftMenu = new LeftMenuFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (fragmentManager.findFragmentById(R.id.menuPanel) != null) {
            fragmentTransaction.replace(R.id.menuPanel, leftMenu, "leftMenu");
        } else {
            fragmentTransaction.add(R.id.menuPanel, leftMenu, "leftMenu");
        }
        fragmentTransaction.commit();
        new ExpandAnimation(slidingPanel, panelWidth1,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.75f, 0, 0.0f, 0, 0.0f);

    } else {
        isExpanded = false;
        // Collapse
        new CollapseAnimation(slidingPanel, panelWidth1,
                TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
                TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
                0, 0.0f);
    }
  }
}

在LeftMenuFragment中,我在左侧菜单滑块中添加了我的魔杖。