【问题标题】:How to set image in LibSlideMenu?如何在 LibSlideMenu 中设置图像?
【发布时间】:2017-02-14 06:25:22
【问题描述】:

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

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>

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

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

【问题讨论】:

  • 在xml布局的header中添加。
  • 滑块的 xml 存在于 Library 中。如果我添加一些东西并 setImageResource 给它,那么它会给我 NullPointerException。

标签: android image android-activity imageview fragment


【解决方案1】:

我得到了解决这个问题的替代方法。我从我的项目中删除了 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 中,我在左侧菜单滑块中添加了我想要的内容。

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 2016-01-23
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多