public class scrollHorizon extends ViewGroup { private Context context; public scrollHorizon(Context context) { super(context); this.context = context; createLayout(); deleteAllView(); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = this.getChildCount(); int childLeft = 0; int childTop = 0; for(int i = 0; i < childCount; i++){ View child = getChildAt(i); child.setVisibility(View.VISIBLE); child.measure(r - l, b - t); child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(),childTop + child.getMeasuredHeight()); if(childLeft <= 320){ childLeft = childLeft + child.getMeasuredWidth(); } else{ childLeft = 0; childTop = childTop + child.getMeasuredHeight(); } } } public void createLayout(){ Resources resource = this.getResources(); PackageManager pmanager = context.getPackageManager(); final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> list = pmanager.queryIntentActivities(mainIntent, 0); for( int i = 0; i < list.size(); i++){ int icon = R.drawable.contacts_button_normal; LinearLayout linear = new LinearLayout(context); linear.setLayoutParams(new LayoutParams(45, 45)); linear.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.icon)); ImageView image2 = new ImageView(context); image2.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.icon)); Drawable drawable = resource.getDrawable(icon); ImageView image = new ImageView(context); image.setBackgroundDrawable(drawable); linear.addView(image2); linear.addView(image); this.addView(linear); } } public void deleteAllView(){ int size = this.getChildCount(); for( int i = 0; i < size; i++){ this.removeViewAt(i); } } }
Android ViewGroup删除子视图时应该注意的一个问题
内容版权声明:除非注明,否则皆为本站原创文章。