自定义导航栏android

时间:2012-02-05 10:26:24

标签: android android-layout

我想做一个自定义水平导航栏,它包含与我有页面一样多的点。

我的想法是:

  1. 创建自定义ListView
  2. 从View
  3. 创建类Dot Dot
  4. 动态地将所有这些点添加到自定义列表视图中。
  5. 这样做是对的吗?

    编辑:

    public class NavigationBarLesson extends LinearLayout {
        private LessonConfig config = LessonConfig.getInstance();
        private ArrayList<NavigationCircle> navigationCircles;
        private int pageCount;
    
        public NavigationBarLesson(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }   
    
        public NavigationBarLesson(Context context) {
            super(context);
            init(context);
        }
    
        private void init(Context context) {
            Log.i("init","yes");
            pageCount = config.getLektionCount();
            navigationCircles = new ArrayList<NavigationCircle>();
    
            for(int i=0; i < pageCount; i++){
                this.addView(new NavigationCircle(context));
            }
    
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            int width = View.MeasureSpec.getSize(widthMeasureSpec);
            int height = View.MeasureSpec.getSize(heightMeasureSpec);
    
            setMeasuredDimension(width, height);
        }
    
    }
    
    public class NavigationCircle extends ImageView{
        private static Bitmap img;
        private Bitmap activeImg;
    
    
        public NavigationCircle(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        public NavigationCircle(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }   
    
        public NavigationCircle(Context context) {
            super(context);
            init();
        }
    
        public void init() {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(0, 0, 5, 0);
            this.setLayoutParams(lp);
            this.setBackgroundResource(R.drawable.upcoming_pages);
        }
    
        public Bitmap getImg() {
            return img;
        }
    
        public Bitmap getActiveImg() {
            return this.activeImg;
        }
    }
    

1 个答案:

答案 0 :(得分:1)

好吧,我不明白为什么你需要一个ListView。创建一个水平的LinearLayout,其中包含一些简单的视图,如TextView或ImageView等,动态添加到LinearLayout。