zoukankan      html  css  js  c++  java
  • 自定义EditText设置图片文字居中

    • JAVA

    自定义类继承EditText or AppCompatEditText

    import android.content.Context;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    import android.support.v7.widget.AppCompatEditText;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.TextView;
    
    public class SearchText extends AppCompatEditText {
    
        private Drawable searchDrawable;
        private int offset;
        private int searchWidth;
        private String hintString;
        private int w;
        private int flag = 0;
    
        public SearchText(Context context) {
            super(context);
            init();
        }
    
        public SearchText(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public SearchText(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        // 获得焦点
                        setTextAlignment(TextView.TEXT_ALIGNMENT_VIEW_START);
                    } else {
                        // 失去焦点
                        setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);
                    }
                }
            });
            setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
            //圈1
            searchWidth = getMeasuredWidth();
            hintString = getHint().toString();
            //圈2
            Paint paint = new Paint();
            Rect rect = new Rect();
            paint.getTextBounds(hintString, 0, hintString.length(), rect);
            w = dip2px(getContext(), rect.width());
            offset = searchWidth / 2 - w * 2;
            if (flag == 0) {
                //圈3  //圈4
                setTextDrawable();
            }
            flag++;
        }
    
        public static int dip2px(Context context, float dpValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dpValue * scale + 0.5f);
        }
    
        @Override
        protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
            super.onTextChanged(text, start, lengthBefore, lengthAfter);
            if (searchDrawable == null) {
                getDrawable();
            }
            if (length() > 0) {
                setTextAlignment(TextView.TEXT_ALIGNMENT_VIEW_START);
                setCompoundDrawables(null, null, null, null);
            } else if (length() == 0) {
                setTextAlignment(TextView.TEXT_ALIGNMENT_CENTER);
                setTextDrawable();
            }
        }
    
        void getDrawable() {
            //圈5
            Drawable[] compoundDrawables = getCompoundDrawables();
            searchDrawable = compoundDrawables[0];
        }
    
        void setTextDrawable() {
            searchDrawable.setBounds(offset, 0, offset + searchDrawable.getIntrinsicWidth(), searchDrawable.getIntrinsicHeight());
            setCompoundDrawables(searchDrawable, null, null, null);
        }
    }
    
    • XML(包名改成自己的)
     <com.*.*.SearchText
            android:id="@+id/work_detail_deal_research"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="@color/white"
            android:layout_marginBottom="2dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:drawableLeft="@drawable/research"
            android:hint="搜索"
            android:textSize="@dimen/txtSize_title"
            android:textColor="@color/txt_color1"/>
    
    • 图标

    search search2x这里写图片描述

    • dimon
    <dimen name="txtSize_title">16dp</dimen>
    

    原文: https://blog.csdn.net/qq_29951983/article/details/78248068

    有梦为马,游历天涯!
  • 相关阅读:
    Docker学习-安装,配置,运行
    Docker学习-从无知到有知的学习过程
    学习记录-java基础部分(一)
    对get post等http请求方式的理解
    Mac和window实现双向数据传输
    git pull时 git cannot lock ref XXXXXX (unable to update local ref)错误解决方案
    三年内我的计划和方向
    关于云服务器和云部署的实操(新手级别入门)
    win7蓝屏死机0x0000003B错误蓝屏故障解决
    JAVA代码:生成一个集合,自定义大小,100以内的随机整数
  • 原文地址:https://www.cnblogs.com/qijianguo/p/10180878.html
Copyright © 2011-2022 走看看