您的位置:首页 > 移动开发 > Android开发

【Android】笔记本

2016-04-01 15:23 489 查看

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());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: