SWBlog icon indicating copy to clipboard operation
SWBlog copied to clipboard

2016.11(context, 四大组件, inflate)

Open yunshuipiao opened this issue 5 years ago • 0 comments

as控制台输出中文乱码问题

在当前模块的buide.gradle文件加入:

tasks.withType(org.gradle.api.tasks.compile.JavaCompile) {
    options.encoding = "UTF-8"
}

context的区别

Application mApp = (Application) getApplication();
Log.e(TAG, "getApp: "+mApp);
Context mContext = getApplicationContext();
Log.e(TAG, "onAppContext: "+mContext);
Context mContext2 = mApp.getApplicationContext();
Log.e(TAG, "onAppContext: "+ mContext2);
Log.e(TAG, "onBaseContext: "+ getBaseContext());
Log.e(TAG, "onThis: "+MainActivity.this);
TextView mTextView = (TextView) findViewById(R.id.textview);
Log.e(TAG, "ongetContentDescription(): "+mTextView.getContentDescription());
Log.e(TAG, "onGetContext: " + mTextView.getContext());

//输出结果
com.example.sw.testproject E/MainActivity: getApp: android.app.Application@fd09430
com.example.sw.testproject E/MainActivity: onAppContext: android.app.Application@fd09430
com.example.sw.testproject E/MainActivity: onAppContext: android.app.Application@fd09430
com.example.sw.testproject E/MainActivity: onBaseContext: android.app.ContextImpl@615e8a9
com.example.sw.testproject E/MainActivity: onThis: com.example.sw.testproject.MainActivity@279780b
com.example.sw.testproject E/MainActivity: ongetContentDescription(): null
com.example.sw.testproject E/MainActivity: onGetContext: com.example.sw.testproject.MainActivity@279780b
//说明:getContext()方式是对于一个activity中的view来说,供其调用,返回这个activity。

参考资料:http://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and

四大组件的工作过程

activity:展示组件, 接收输入信息和交互,可显示或者隐式Intent启动触发, 前台界面的角色。 Service:计算性组件。有启动和绑定状态,运行在主线程中。 BroadcastRecevier:消息型组件。也叫广播,静态注册和动态注册。 ContentProvider:数据共享型组件。

Intent传递数据的两种方式:

bundle和序列化parcelable。

###判断网络连接状态

    private void checkNetworkConnection() {
      // BEGIN_INCLUDE(connect)
      ConnectivityManager connMgr =
          (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
      if (activeInfo != null && activeInfo.isConnected()) {
          wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
          mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
          if(wifiConnected) {
              Log.i(TAG, getString(R.string.wifi_connection));
          } else if (mobileConnected){
              Log.i(TAG, getString(R.string.mobile_connection));
          }
      } else {
          Log.i(TAG, getString(R.string.no_wifi_or_mobile));
      }
      // END_INCLUDE(connect)
    }

spannable定义下划线的超链接

String licenseTips = getString(R.string.app_license_tips);
SpannableString ss = new SpannableString(licenseTips);
ss.setSpan(new UnderlineSpan(), 7, licenseTips.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mLicenseTips.setText(ss);

###获取设备宽高

DisplayMetrics dm = getResources().getDisplayMetrics();
int w_screen = dm.widthPixels;
int h_screen = dm.heightPixels;

处理自动问题:

定时器,具体涉及到的工具类,Timer, TimerTask, Handle 现在全部可用Rxjava2代替。

setContentView()

setContentView()方法的内部也是用LayoutInflater来加载布局的,只不过这部分源码是internal的。 layoutInflater的实例之后就可以调用他的inflater方法来加载布局。 layoutInflater.inflate(resouceId, root) inflate(int resource, ViewGroup parent, boolean attachToRoot): 根据参数名判断用法。

mContentView = LayoutInflater.from(getActivity().inflate(R.layout.fragment_home_contact, null)); //从指定xml加载一个view

自定义一个自动换行的标签页布局

需要重写onMeasure()测量自身宽度,重写onLayout(),算出每个子view的大小,联合屏幕高度进行计算。

//子view的类型和宽高可以自己设定。
public class AutoBreakViewGroup extends ViewGroup {

    private int mScreenWidth;
    private int mHorizontalSpacing;
    private int mVerticalSpacing;

    public AutoBreakViewGroup(Context context) {
        super(context);
        init();
    }

    public AutoBreakViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AutoBreakViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        mScreenWidth = dm.widthPixels;
    }

    public void setmHorizontalSpacing(int mHorizontalSpacing) {
        this.mHorizontalSpacing = mHorizontalSpacing;
    }

    public void setmVerticalSpacing(int mVerticalSpacing) {
        this.mVerticalSpacing = mVerticalSpacing;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int mTotalHeight = 0;
        int mTotalWidth = 0;

        int mTempHeight = 0;

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            int measureHeight = childView.getMeasuredHeight();
            int measureWidth = childView.getMeasuredWidth();
            mTempHeight = (measureHeight > mTempHeight) ? measureHeight : mTempHeight;
            if ((measureWidth + mTotalWidth + mHorizontalSpacing) > mScreenWidth) {
                mTotalWidth = 0;
                mTotalHeight += (mTempHeight + mVerticalSpacing);
                mTempHeight = 0;
            }
            childView.layout(mTotalWidth + mHorizontalSpacing, mTotalHeight,
                    measureWidth + mTotalWidth + mHorizontalSpacing, mTotalHeight + measureHeight);
            mTotalWidth += (measureWidth + mHorizontalSpacing);
        }
    }
}

可用google的flexlayout布局替换, 不必重写view。

android网络相关

(http://blog.csdn.net/itachi85/article/details/50982995:网络)

yunshuipiao avatar Apr 27 '19 14:04 yunshuipiao