美文网首页
不断向上滚动的公告栏

不断向上滚动的公告栏

作者: pdog18 | 来源:发表于2017-03-21 17:08 被阅读23次

    不知道该如何控制TextView的 TextSize 和TextView大小
    用inflate 填充xml布局?
    求大神指点下,为什么我获取我的控件的高设置给TextView ,TextView却比期望的大出好多

    ddd.gif

    使用

    
      MyViewFlipper myViewFlipper = (MyViewFlipper) findViewById(R.id.mvf);
            myViewFlipper.setStrings(titleList);
    
    
            myViewFlipper.setOnItemClickListener(new MyViewFlipper.ItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Toast.makeText(ViewFlipperActivity.this, "position" + position, Toast.LENGTH_SHORT).show();
                }
            });
    
    
    /**
     * desc:
     * author: pdog
     * email: pdog18@foxmail.com
     * time: 2017/3/21  14 :33
     */
    public class MyViewFlipper extends ViewFlipper implements View.OnClickListener {
    
        private String DEFAULT_MESSAGE = "Default Message";
        private int mCurrPos;       //当前显示的
        private ArrayList<String> mStrings;
        private Timer mTimer = new Timer();
        private TextView mTextView;
    
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                post(new Runnable() {
                    @Override
                    public void run() {
                        moveNext();
                    }
                });
            }
        };
    
        TranslateAnimation in = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f);
    
        TranslateAnimation out = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, -1.0f);
    
        public MyViewFlipper(Context context) {
            this(context, null);
        }
    
        public MyViewFlipper(Context context, AttributeSet attrs) {
            super(context, attrs);
            setOnClickListener(this);
        }
    
        @Override
        protected void onAttachedToWindow() {
            super.onAttachedToWindow();
            in.setDuration(500);
            out.setDuration(500);
            mTimer.schedule(task, 0, 4000);
        }
    
        @Override
        protected void onDetachedFromWindow() {
            super.onDetachedFromWindow();
            mTimer.cancel();
        }
    
        /**
         * 需要显示的数组
         *
         * @param strings
         */
        public void setStrings(@NonNull ArrayList<String> strings) {
            if (strings == null) {
                throw new NullPointerException("传入的集合为null");
            }
            this.mStrings = strings;
        }
    
    
        private void moveNext() {
            setView(mCurrPos, mCurrPos + 1);
            setInAnimation(in);
            setOutAnimation(out);
            showNext();
        }
    
      private void setView(int curr, int next) {
            if (mStrings == null || mStrings.size() == 0) {
                if (mTextView == null) {
                    mTextView = new TextView(getContext());
                    addView(mTextView);
                }
                mTextView.setText(DEFAULT_MESSAGE);
                return;
            }
            View view = LayoutInflater.from(getContext()).inflate(R.layout.viewflipper_item, this, false);
            ImageView image = (ImageView) view.findViewById(R.id.img_viewflipper_item);
            TextView tv = (TextView) view.findViewById(R.id.tv_viewflipper_item);
    
            if ((curr < next) && (next > (mStrings.size() - 1))) {
                next = 0;
            } else if ((curr > next) && (next < 0)) {
                next = mStrings.size() - 1;
            }
            tv.setText(mStrings.get(next));
            if (getChildCount() > 1) {
                removeViewAt(0);
            }
            addView(view, getChildCount());
            mCurrPos = next;
        }
    
        ItemClickListener mItemClickListener;
    
        public void setOnItemClickListener(ItemClickListener listener) {
            this.mItemClickListener = listener;
        }
    
        interface ItemClickListener {
            void onItemClick(int position);
        }
    
        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(mCurrPos);
            }
        }
    }
    
    
    
    <?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="40dp"
                  android:orientation="horizontal">\
    
        <ImageView
            android:id="@+id/img_viewflipper_item"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:background="@mipmap/ic_launcher"/>
    
        <TextView
            android:id="@+id/tv_viewflipper_item"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center_vertical"
            android:text="@string/app_name"
            android:textSize="30sp"/>
    </LinearLayout>
    

    相关文章

      网友评论

          本文标题:不断向上滚动的公告栏

          本文链接:https://www.haomeiwen.com/subject/sbugnttx.html