Android开发遇到的问题和小知识总结(不断更新中)

WebView

WebView加载不出来图片的时候可以添加属性 webSettings.setDomStorageEnabled(true);

部分手机上WebView.goback()方法返回时onReceivedTitle方法接收不到标题,可以在onPageFinished()方法中设置标题,通过WebView.getTitle()。

动态设置全屏

 private void setFullScreen(boolean enable) {
   if (enable) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(lp);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            WindowManager.LayoutParams attr = getWindow().getAttributes();
            attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attr);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

Activity之间传递数据,intent为空的情况

eg: FirstActivity—>SecondActivity

在SecondActivity关闭的方法里 setResult(…)要在finish()和super.onBackPressed(…)方法之前执行。

注意:只有当前Activity被finish掉,结果才会被发送给上一个Activity的onActivityResult去处理。

在Activity中设置屏幕背景颜色变暗

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.alpha = 0.7f;
getWindow().setAttributes(layoutParams);

设置颜色 sdk23及以上 Context.getResourses().getColor(id)过时问题

更改为ContextCompat.getColor(context, R.color.your_color);

ContextCompat在v4包中,所以低版本也可以用

设置输入法隐藏

Activity设置属性

android:windowSoftInputMode="stateAlwaysHidden"

Activity中添加代码

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)
inputMethodManager.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);

EditText透明

<EditText
    android:layout_width="40dp"
    android:layout_height="match_parent"
    android:layout_gravity="end|center_vertical"
    android:background="@android:color/transparent"
    android:textColorHint="@android:color/transparent"
    android:textColor="@android:color/transparent"
    android:cursorVisible="false"
    android:inputType="textUri|textMultiLine"
    />

正则根据回车符分割字符串

Pattern pattern = Pattern.compile("\n");
String[] strs = pattern.split(str);
if (strs != null && strs.length > 0)
{
    compareCodeList(strs[strs.length - 1]);
}

Android横竖屏切换Activity重启问题

为了在横竖屏切换的时候不重新创建Activity,可以使用下面方式:

在AndroidManifest.xml文件中添加Activity属性为 android:configChanges=“orientation|keyboardHidden|screenSize”

重写onConfigureChanged()方法,系统在切换横竖屏时会调用此方法,而不是Activity的生命周期方法。

Home键监听

class HomeReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
				String reason = intent.getStringExtra("reason");
				if (reason != null) {
					if (reason.equals("homekey")) {//短按home键
					    doSomething();
					}else if(reason.equals("recentapps")){//长按home键
					    doSomething();
                    }
				}
			}
		}
	}

Activity中调用

HomeReceiver receiver = new HomeReceiver();
registerReceiver(receiver, new IntentFilter(
				Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
unregisterReceiver(receiver);

锁屏监听

private ScreenBroadcastReceiver mScreenReceiver;
private class ScreenBroadcastReceiver extends BroadcastReceiver {
    private String action = null;
    @Override
    public void onReceive(Context context, Intent intent) {
        action = intent.getAction();
        if (Intent.ACTION_SCREEN_ON.equals(action)) { 
            // 开屏
        } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { 
            // 锁屏
        } else if (Intent.ACTION_USER_PRESENT.equals(action)) {         // 解锁
        }
    }
}
private void startScreenBroadcastReceiver() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_USER_PRESENT);
    context.registerReceiver(mScreenReceiver, filter);
}

ViewPager(onPageChangeListener)

    @Override
	public void onPageScrolled(int position, float positionOffset,
		int positionOffsetPixels) {
			params.leftMargin = (int) ((position+positionOffset)*position_one+(position_one-bottomLineWidth)/2);
			bottomLine.setLayoutParams(params);
		}
		//position_one  screenWidth/count(title个数)  
		//(position_one-bottomLineWidth)/2)  bottomLine左边距
		//params bottomLine的LayoutParams	

android.view.InflateException: Binary XML file line #8: Error inflating class com.facebook.drawee.view.SimpleDraweeView

问题原因:Fresco未初始化

解决方法:Fresco.initialize(MainActivity.this);

关于android应用

android应用根据包名区分唯一性,微信平台支付绑定一个包名,百度地图可以绑定两个(一个测试,一个正式),所以测试支付的话如果是不同的包名需要另外配置才可以测试。另外如果想在手机上同时安装测试版和正式版的话,设置不同的包名就可以了。

导入项目不动

1.修改gradle文件和当前能运行的项目一致 根目录/gradle/wrapper/gradle-wrapper.properties

2.修改项目gradle文件的gradle版本

七牛下载图片被裁减问题

项目中用七牛云存储图片,获取到三个大小的图片(large,medium,small),其中只有large图片与原图一致,其余皆被裁减后返回,经查看是图片的设置出现了问题,后台通过七牛的imageView2接口获取图片资源,接口参数mode设置错误,修改后即可解决问题。

android:clipChildren属性不起作用问题

android:clipChildren属性控制Android的一个子view是否运行超越父控件边界放置,设置为false后那么当子控件的高度高于父控件时 也会完全显示,而不会被压缩。默认设置为true。

使用时在当前xml文件的根布局和需要控制的子控件的父控件处设置改属性。

实际使用时发现按照说明写好后未起作用,试过将父控件和根布局从RelativeLayout改为FrameLayout皆未起作用,另外加上了android:clipToPadding="false"属性亦未起作用,最后在stackoverflow上看到有建议将父控件的padding属性去掉,终于实现想要的效果。

另外发现子控件超出父控件的部分点击事件不起作用,不知道还有没有人遇到这个问题,如果有的话,希望可以交流一下。

循环播放旋转动画

刚开始出现了重复播放动画卡顿的问题,加上这句代码**rotation.setInterpolator(new LinearInterpolator());**代码后得以解决

ObjectAnimator rotation = ObjectAnimator.ofFloat(ivImg, "rotation", 0f, 360f);
        rotation.setRepeatCount(ValueAnimator.INFINITE);
rotation.setInterpolator(new LinearInterpolator());
rotation.setDuration(1000);
rotation.start();

OS X - Can’t start Git: /usr/bin/git Probably the path to Git executable is not valid

更新Mac系统后Android Studio报了这个错,解决办法如下所示:

Git 官网上重新下载一个最新的git软件,安装

Terminal中打which git查看git路径,复制git路径

在Android Studio设置上一步复制的git路径

over
###从Github上clone项目到Android Studio,ssh方式提示“git Could not read from remote repository”,https方式测试可以通过,iterm测试ssh方式可以通过。
解决方法:Android Studio–》Preferences–》Version Control–》Git–》SSH executable–》选择Native(默认Built-in)
###Java格式化字符串
####转换符
%s->“哈哈”
%d->3
%f->99.99(%.2f 保留两位小数)
%%->%
%n->换行符

标志

“+”> %+d (为正数或者负数添加符号 eg:+99、-99)
“0”>%03d(数字前面补0 eg::007)

SSL peer shut down incorrectly

导入新项目编译时报了这个错,原因是Project.gradle classpath中的gradle版本不一致导致的,修改gradle版本与新建项目一致即可

查看apk签名信息

网上推荐是用keytool工具来查看,但是运行后没有md5值,没有搜到解决办法,只好换了种方式。
在Android Studio中,在右侧找到菜单栏“Gradle”—>”:app”—>“Tasks”—>“android”—>双击“signReport”即可。这时在Terminal中可以查看debug版本、resalse版本的SHA1和MD5值。见截图
查看签名信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值