2017-06-26 21 views
1

Screenshot of Listview link. 我正在做一个菜单显示,并希望将图像设置到我的列表视图。但是,图像设置了两倍。我不知道为什么? 在MenuAdapter类我getView功能:为什么Listview中的图像设置为双倍?

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    Holder holder; 
    if(convertView == null) { 
     holder = new Holder(); 
     convertView = inflater.inflate(R.layout.menu_lists, null); 
     holder.tv=(TextView) convertView.findViewById(R.id.music_txt); 
     holder.imageView=(ImageView) convertView.findViewById(R.id.music_icon); 
     convertView.setTag(holder); 
    } else { 
     holder = (Holder) convertView.getTag(); 
    } 

    holder.tv.setText(result[position]); 
    holder.tv.setTextColor(Color.RED); 

    holder.imageView.setImageResource(image[position]); 


    convertView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show(); 
     } 
    }); 
    return convertView; 
} 

这是layout.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="wrap_content" 
 
    android:paddingBottom="@dimen/activity_vertical_margin" 
 
    android:orientation="vertical"> 
 

 
    <LinearLayout 
 
     android:orientation="horizontal" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:clickable="false" 
 
     android:background="#aaef79" 
 
     android:id="@+id/music"> 
 

 
     <ImageView 
 
      android:layout_width="match_parent" 
 
      android:layout_height="match_parent" 
 
      android:id="@+id/music_icon" 
 
      android:background="@mipmap/ic_launcher" 
 
      android:layout_weight="3" 
 
      android:layout_gravity="center" /> 
 

 
     <TextView 
 
      android:layout_width="match_parent" 
 
      android:layout_height="match_parent" 
 
      android:textAppearance="?android:attr/textAppearanceLarge" 
 
      android:text="Music" 
 
      android:id="@+id/music_txt" 
 
      android:layout_weight="1" 
 
      android:gravity="fill_vertical|center_horizontal" /> 
 

 
    </LinearLayout> 
 

 
</LinearLayout>

回答

3

,因为你在XML设置android:background="@mipmap/ic_launcher"和适配器

设置 setImageResource

删除XML中的android:background="@mipmap/ic_launcher或chang e到android:src="@mipmap/ic_launcher

实际上每个ImageView都可以在xml中同时使用backgroundsrcsetImageResource意味着你编程设置为src你的观点和setBackgroundResource意味着你编程设置为background视图

+0

感谢的人,我只是没有注意到它)))) –

+0

好运:d :) – MHP

相关问题