android 小知识

1.边框 shap

    android:background="@drawable/......."
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- view背景色 -->
    <solid android:color="@color/_ffffff_bg" />
    <!-- 边框颜色 宽度 -->
    <stroke
        android:width="5dp"
        android:color="@color/_f0f1f5_bg" />
    <!-- 边框圆角 -->
    <corners
        android:topLeftRadius="10dp"
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topRightRadius="10dp" />
</shape>

2.button 去掉阴影 高版本会有

style=”?android:attr/borderlessButtonStyle”

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
style="?android:attr/borderlessButtonStyle" />

EditText 不自动获取焦点
android:focusable=”true”
android:focusableInTouchMode=”true”

   <LinearLayout
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/_ffffff_bg"
            android:orientation="vertical">


            <EditText
                android:paddingRight="12dp"
                android:singleLine="true"
                android:gravity="right|center_vertical"
                android:id="@+id/value_1_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                android:hint="请输入"
                android:paddingLeft="8dp"
                android:textColor="@color/_999999_word"
                android:textColorHint="#b0b0b0"
                android:textSize="15sp" />


        </LinearLayout>

3.EditText默认不弹出软键盘的方法

方法一: 在Activity中OnCreate方法里面加下面这句代码
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);隐藏软键盘。这是我用的方法

别人用的这个说是可以
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
但在我这儿一直就隐藏掉了。

方法二:
可以在edittext的父布局结构中(例如LinearLayout,RelativeLayout等)添加

android:focusable=”true”
android:focusableInTouchMode=”true”

这个方法也是可行的,只是我的edittext写在dialog里面,没试出来,别的地方可以。
还有修改AndroidMainfest.xml文件android:windowSoftInputMode=”stateAlwaysHidden”和强制隐藏Android输入法窗口
例如:
EditText edit=(EditText)findViewById(R.id.edit);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
这两个我都没有试出来,也有可能是写在dialog的原因
还有就是去掉焦点,也是可以的。

4.EditText 光标放到末尾 (注意获取焦点)

方法:edittext.setSelection(int);
et.setText(content);//设置EditText控件的内容
et.setSelection(content.length());//将光标移至文字末尾

5.EditText键盘弹出 ,控件上移动

方法一:.还有修改AndroidMainfest.xml文件 activity
android:windowSoftInputMode=”adjustResize”

方法二:
   boolean haveChanged = false;
    int heightDiff;
    private boolean isKeyboardShown(View rootView) {
        final int softKeyboardHeight = 100;
        Rect r = new Rect();
        rootView.getWindowVisibleDisplayFrame(r);
        DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
        heightDiff = rootView.getBottom() - r.bottom;
        Log.e("hahah", heightDiff + "");
        return heightDiff > softKeyboardHeight * dm.density;
    }


    //rootView  为 editText    group_ll  为 要移动控件
    private void setListenerToRootView(final View rootView) {

        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                boolean isOpen = isKeyboardShown(rootView.getRootView());
                if (isOpen) {

                    ObjectAnimator.ofFloat(group_ll, "translationY", 0, -heightDiff).setDuration(100).start();
                    haveChanged = true;

                } else {
                    if (haveChanged) {
                        ObjectAnimator.ofFloat(group_ll, "translationY", -heightDiff, 0).setDuration(100).start();
                    }
                }
            }
        });
    }

6.CheckBox 样式 更改为自己图片



        <CheckBox
            android:button="@null"
            android:background="@drawable/checkbox_back_list_selector"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_margin="12dp"
            android:id="@+id/checkBox1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:clickable="false"
            android:focusable="false" />

selector 标签

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/image_on" />
    <item android:state_selected="true" android:drawable="@drawable/image_on"></item>
    <item android:state_pressed="true" android:drawable="@drawable/image_on"></item>
    <item android:state_checked="false" android:drawable="@drawable/image_off" />

</selector>


7. 获取屏幕宽高


    // 获得屏幕宽高
    public void getAndroiodScreenProperty() {
        WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度(像素)
        int height = dm.heightPixels;       // 屏幕高度(像素)
        float density = dm.density;         // 屏幕密度(0.75 / 1.0 / 1.5)
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi(120 / 160 / 240)
        // 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        int screenHeight = (int) (height / density);// 屏幕高度(dp)

        Log.d("h_bl", "屏幕宽度(像素):" + width);
        Log.d("h_bl", "屏幕高度(像素):" + height);
        Log.d("h_bl", "屏幕密度(0.75 / 1.0 / 1.5):" + density);
        Log.d("h_bl", "屏幕密度dpi(120 / 160 / 240):" + densityDpi);
        Log.d("h_bl", "屏幕宽度(dp):" + screenWidth);
        Log.d("h_bl", "屏幕高度(dp):" + screenHeight);
    }

8.竖直的虚线


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by hexiang on 17/11/14.
 */

public class ImaginaryLineView extends View {

    private Context ct;
    private Paint mPaint;
    private Path mPath;
    private PathEffect effects;
    private int width;
    private int height;

    private int defaultColor=0xffe0e0e0;

    public ImaginaryLineView(Context context) {
        this(context, null);
    }

    public ImaginaryLineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ImaginaryLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        ct = context;
        init();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;

    }

    private void init() {

        //初始化,并打开抗锯齿
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(defaultColor);
        mPaint.setStrokeWidth(dip2px(ct, 1));

        mPath = new Path();
        //数组含义:里面最少要有2个值,值的个数必须是偶数个。偶数位(包含0),表示实线长度,奇数位表示断开的长度
        effects = new DashPathEffect(new float[]{4, 4}, 0);


    }

    /**
     * 设置线的必要属性
     *
     * @param color 十六进制颜色值
     * @param lineWidth 虚线宽度,单位是dp
     */
    public void setLineAttribute(int color, float lineWidth,float[] f) {

        if (color == 0) {
            color = defaultColor;
        }
        if (lineWidth == 0) {
            lineWidth = 1;
        }
        if(f==null){
            f=new float[]{4,2};
        }
        effects = new DashPathEffect(f, 0);

        mPaint.setStrokeWidth(dip2px(ct, lineWidth));
        mPaint.setColor(color);

        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //定义起点
        mPath.moveTo(0, 0);
        //定义终点
        if(width>height){
            //宽度比高度大,是横线
            mPath.lineTo(width, 0);
        }else{
            //竖线。(根据实际情况,这里不考虑宽高相等情况)
            mPath.lineTo(0, height);
        }

        mPaint.setPathEffect(effects);

        canvas.drawPath(mPath, mPaint);
    }

    private static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }



}

9.虚线边框

android:background=”@drawable/dashed_frame_shap”

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--背景-->
    <solid android:color="@color/colorTransparent"/>
    <!--虚线-->
    <stroke
        android:width="1dp"
        android:dashWidth="10dp"
        android:color="@color/_cccccc"
        android:dashGap="5dp"/>
    <size android:height="2dp"/>
</shape>

android 中的颜色相关知识
Android中的颜色相关内容包括颜色模式,创建颜色的方式,以及颜色的混合模式等
1 在java中定义颜色

//java中使用Color类定义颜色
  int color = Color.GRAY;     //灰色
  //Color类是使用ARGB值进行表示
  int color = Color.argb(127, 255, 0, 0);   //半透明红色
  int color = 0xaaff0000;

2 在xml文件中定义颜色
在xml文件中以”#“开头定义颜色,后面跟十六进制的值,有如下几种定义方式

  #f00            //低精度 - 不带透明通道红色
  #af00           //低精度 - 带透明通道红色

  #ff0000         //高精度 - 不带透明通道红色
  #aaff0000       //高精度 - 带透明通道红色
<?xml version="1.0" encoding="utf-8"?>
<resources>

    //定义了红色(没有alpha(透明)通道)
    <color name="red">#ff0000</color>
    //定义了蓝色(没有alpha(透明)通道)
    <color name="green">#00ff00</color>
</resources>

3 在java文件中引用xml中定义的颜色

//方法1
int color = getResources().getColor(R.color.mycolor);

//方法2(API 23及以上)
int color = getColor(R.color.myColor);  

4 在xml文件(layout或style)中引用或者创建颜色

<!--在style文件中引用-->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/red</item>
    </style>

 <!--在layout文件中引用在/res/values/color.xml中定义的颜色-->
  android:background="@color/red"     

 <!--在layout文件中创建并使用颜色-->
  android:background="#ff0000"

android view 坐标正确理解

Top:子View上边界到父view上边界的距离
Left:子View左边界到父view左边界的距离
Bottom:子View下边距到父View上边界的距离
Right:子View右边界到父view左边界的距离

记忆方式
Top:子View左上角距父View顶部的距离;
Left:子View左上角距父View左侧的距离;
Bottom:子View右下角距父View顶部的距离
Right:子View右下角距父View左侧的距离

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值