基本UI空控件的用方法和属性

控件使用

ViewFlipper

  • setInAnimation/setOutAnimation
  • showNext/showPrevious
  • getDisplayedChild() ==> index ==> getChildAt
  • addView(view)/addView(view,position)

AbsListView

  • setSelector(new ColorDrawable(Color.TRANSPARENT));

GridView

  • setNumColumns;
  • setVerticalSpacing/setHorizontalSpacing

AnimationSet = AnimationUtils.loadAnimation()

Tip: 在GridView 的Adapter 内的View 设置setOnClickListener 会导致 设置的 GestureDetector.SimpleOnGestureListener.onFling返回的MotionEvent == null;

EditText

  • 显示/隐藏密码
    setInputType(hasShowPassword ? 129 : InputType.TYPE_TEXT_VARIATION_PASSWORD);
  • 光标位置(定位到最后)
    edittext.setSelection(int)
  • 获取焦点
    editText.requestFocus();//获取焦点
    如果对edittext组件设置了editText.setFocusable(false);需要重新获取焦点则必须执行:
    editText.setFocusable(ture);
    editText.setFocusableInTouchMode(true);
    editText.requestFocus();
    注意:这种情况下,当重新点击文本框,是无法打开软键盘,必须点击第二次才能打开。

ViewPager

  • setPageTransformer(); //设置页面切换效果 JazzyViewPager 提供11种切换效果

  • 经常直播的效果

    public class DepthPageTransformer implements PageTransformer  {
      private static final float MIN_SCALE = 0.75f;
    
      public void transformPage(View view, float position) {
          int pageWidth = view.getWidth();
    
          if (position < -1) { // [-Infinity,-1) ///看不到的一页 *
              // This page is way off-screen to the left.
              view.setAlpha(0);
    
          } else if (position <= 0) { // [-1,0]
              // Use the default slide transition when moving to the left page
              view.setAlpha(1);
              view.setTranslationX(0);
              view.setScaleX(1);
              view.setScaleY(1);
    
          } else if (position <= 1) { // (0,1]
              // Fade the page out.
              view.setAlpha(1 - position);
    
              // Counteract the default slide transition
              view.setTranslationX(pageWidth * -position);
    
              // Scale the page down (between MIN_SCALE and 1)
              float scaleFactor = MIN_SCALE
                      + (1 - MIN_SCALE) * (1 - Math.abs(position));
              view.setScaleX(scaleFactor);
              view.setScaleY(scaleFactor);
    
          } else { // (1,+Infinity]
              // This page is way off-screen to the right.
              view.setAlpha(0);
          }
      }
    }

ListView

  • List内容平滑的移动到指定的位置 smoothScrollToPosition(position);
  • 一定要设置的:View contentView,int width, int height;

    //构造方法:
    public PopupWindow (Context context)
    public PopupWindow(View contentView)
    public PopupWindow(View contentView, int width, int height)
    public PopupWindow(View contentView, int width, int height, boolean focusable)
    //常用的初始化
    View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);
    PopupWindow popWnd = PopupWindow (context);
    popWnd.setContentView(contentView);
    popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
  • 显示

    //相对某个控件的位置(正左下方),无偏移
    showAsDropDown(View anchor)//相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
    showAsDropDown(View anchor, int xoff, int yoff)//相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
    showAtLocation(View parent, int gravity, int x, int y)
  • 添加动画
    mPopWindow.setAnimationStyle(R.style.contextMenuAnim);
    contextMenuAnim.xml

    <style name="contextMenuAnim" parent="@android:style/Animation.Activity">  
      <item name="android:windowEnterAnimation">@anim/context_menu_enter</item>  
      <item name="android:windowExitAnimation">@anim/context_menu_exit</item>  
    </style>  

    context_menu_exit.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <set xmlns:android="http://schemas.android.com/apk/res/android" >  
    
      <translate  
          android:duration="@android:integer/config_shortAnimTime"  
          android:fromXDelta="0"  
          android:fromYDelta="0"  
          android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
          android:toXDelta="0"  
          android:toYDelta="100%p" />  
    </set>  
  • setFocusable EditText 是否点击后可编辑

  • mPopWindow.setBackgroundDrawable(new BitmapDrawable()) 的作用

    1. setOutsideTouchable()才会生效
  1. PopupWindow才会对手机的返回按钮有响应
  • API >= 23 setEnterTransition(Transition) or setExitTransition(Transition)

参考:

  1. PopupWindow才会对手机的返回按钮有响应

常用的UI方法

获取xml属性

db转px px转db 像素转换

  // Calculate ActionBar height
  TypedValue tv = new TypedValue();
            if (context.getTheme().resolveAttribute(
                    android.R.attr.actionBarSize, tv, true)) {
                mActionBarHeight = TypedValue.complexToDimensionPixelSize(
                        tv.data, context.getResources().getDisplayMetrics());
            }

DensityUtil

package com.lwj.lib.utils;

import android.content.Context;

public class DensityUtil {

    /** 
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
     */  
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);  
    }  

    /** 
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
     */  
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);  
    }  
}