1.设置Android软键盘弹出时不挤压屏幕,或者让界面整体上移:
AndroidManifest.xml文件中界面对应的<activity>里加入
android:windowSoftInputMode="adjustPan" 键盘就会覆盖屏幕
android:windowSoftInputMode="stateVisible|adjustResize" 屏幕整体上移
2.屏幕旋转时,取消再次加载activity:
在AndroidManifest.xml中对应的<activity>中加入:
android:configChanges="orientation|screenSize"
3.锁定某个activity横屏或者竖屏:
同样也是在Manifest.xml中:
android:screenOrientation="portrait" 竖屏
android:screenOrientation="landscape " 横屏
android:screenOrientation="unspecified" 未指明方向
在代码中设置横屏:
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
4.进入到一个activity中禁止弹出输入法:
在Manifest.xml的对应的activity中添加如下两行:
android:windowSoftInputMode="adjustUnspecified|stateHidden"
android:configChanges="orientation|keyboardHidden"
5.布局之shape
设置按钮的背景色:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="#f75000" />
<stroke android:width="2dp" android:color="#fad3cf" />
<corners android:radius="10dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape>
</item>
<item android:state_enabled="true">
<shape>
<gradient android:angle="270" android:endColor="#ff9d77" android:startColor="#ff9d77" />
<stroke android:width="2dp" android:color="#fad3cf" />
<corners android:radius="10dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#aaaaaa" />
<stroke android:width="2dp" android:color="#dcdcdc" />
<corners android:radius="10dp" />
<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape>
</item>
</selector>
说明:
gradient -- 对应颜色渐变。 startcolor、endcolor就不多说了。
android:angle 是指从哪个角度开始变。
solid -- 填充。
stroke -- 描边。
corners -- 圆角。
padding -- 定义内容离边界的距离。
设置虚线:
<stroke
android:dashGap="3dp"
android:dashWidth="6dp"
android:width="1dp"
android:color="#63a219" />
dashgap:是虚线每一段的间隔。dashwidth是虚线每一段的长度。
6.判断设备是手机还是平板
private boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
7.修改gitignore之后如何让他生效:
改动过.gitignore文件之后,在repo的根目录下运行:
git rm -r --cached .
git add .
之后可以进行提交:
git commit -m "fixed untracked files"
8.解决url中有中文时无法显示图片的问题:
String encodeurl = URLEncoder.encode(oldurl, "utf-8").replaceAll("\\+", "%20");
encodeurl = encodeurl.replaceAll("%3A", ":").replaceAll("%2F", "/");
直接用encodeurl。
9.获取当前时间:
方法1:
long time=System.currentTimeMillis();
方法2:
final Calendar mCalendar=Calendar.getInstance();
mCalendar.setTimeInMillis(time);
取得小时:mHour=mCalendar.get(Calendar.HOUR);
取得分钟:mMinuts=mCalendar.get(Calendar.MINUTE);
方法3:
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
t.setToNow(); // 取得系统时间。
int year = t.year;
int month = t.month;
int date = t.monthDay;
int hour = t.hour; // 0-23
方法4:
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());
10.设置自定义Dialog的大小:
MyDialog myDialog = new MyDialog(MainActivity.this, R.style.myDialogStyle, OpenType.Search);
myDialog.show();
Window dialogWindow = myDialog.getWindow();
WindowManager m =getWindowManager();
Display d = m.getDefaultDisplay(); // 获取屏幕宽、高度
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
p.height = (int) (d.getHeight() * 1); // 高度设置为屏幕的0.6,根据实际情况调整
p.width = (int) (d.getWidth() * 1); // 宽度设置为屏幕的0.65,根据实际情况调整
dialogWindow.setAttributes(p);
注意:要先show()然后再设置大小。不过有些情况下可能需要先设置大小然后再show()。
11.Image控件的ScaleType属性里各个的意思是什么:
12.自定义Style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item><!--边框-->
<item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">false</item><!--半透明-->
<item name="android:windowNoTitle">true</item><!--无标题-->
<item name="android:windowBackground">@color/transparent</item><!--背景透明-->
<item name="android:backgroundDimEnabled">false</item><!--模糊-->
</style>
</resources>