5、显示当前时间:
SimpleDateFormat sdf = new SimpleDateFormat( " HH:mm:ss " );
// 将当前时间显示在TextView组件中
tvTime.setText( " 当前时间: " + sdf.format( new Date()));
6、代码添加布局文件
代码
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64 );
TextView text = new TextView(activity);
text.setLayoutParams(lp);
text.setTextSize( 20 );
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding( 36 , 0 , 0 , 0 );
text.setText(s);
7、自定义窗口标题栏
代码
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 自定义标题栏
setContentView(R.layout.main);
// 为标题栏设置一个xml布局
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_custom);
8、Activity仿Dialog Theme(加图标和标题其实就是Activity的icon和titile)
(1)、自定义样式
< style name =“Theme.CustomDialog” parent =“android:style/Theme.Dialog” >
< item name =“android:windowBackground” > @drawable/filled_box </ item >
</ style >
@drawable/filled_box:资源文件中新建drawable文件夹,新建filled_box样式xml
代码
< shape xmlns:android =“http://schemas.android.com/apk/res/android” >
< solid android:color ="#f0600000" />
< stroke android:width =“3dp” color ="#ffff8080" />
< corners android:radius =“3dp” />
< padding android:left =“10dp” android:top =“10dp”
android:right =“10dp” android:bottom =“10dp” />
</ shape >
(2)Manifest.xml中配置Acitivity样式
代码
< activity android:name =".Main"
android:label ="@string/app_name"
android:theme ="@android:style/Theme.Dialog" >
< intent-filter >
< action android:name =“android.intent.action.MAIN” />
< category android:name =“android.intent.category.LAUNCHER” />
</ intent-filter >
</ activity >
(3)为Activity设置图标
代码
// 设定窗口模式(仿Dialog中的icon,带有一个左图标)
requestWindowFeature(Window.FEAT
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
URE_LEFT_ICON);
setContentView(R.layout.main);
// 设置图片资源
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON
, R.drawable.icon);
9、从一个Activity1到另一个Activity2,当在第二个Activity2按返回键不出现Activity1的做法是在打开Activity2的同时关闭
Activity1
Intent intent = new Intent(Main. this , fowardwidget. class );
startActivity(intent);
finish();
10、允许TextView的文本值拼接
(1)允许在TextView的文本值后添加buffer text
tv.setText(tv.getText(),TextView.BufferType.EDITABLE);
(2)使用Editable对象添加buffer text
Editable text= (Editable)tv.getText();
text.append( " editable " );
text.append( " /n " ):
11、如果历史栈中包含Activity,打开此Activity从栈中放到栈顶层而不是从新打开Activity
Intent intent = new Intent(ReorderFour. this , ReorderTwo. class );
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
12、避免输入法面板遮挡,在manifest.xml中activity中设置android:windowSoftInputMode
android:windowSoftInputMode=“stateVisible|adjustResize”
13、获取当前手机壁纸和设置手机壁纸(wallpaper)
(1)获取当前壁纸
WallpaperManager wm=WallpaperManager.getInstance(this);
Drawable wallpaper=wpm.getDrawable();
(2)设置当前壁纸,同时要添加壁纸设置权限
imapaper.setDrawingCacheEnabled(true);
Drawable drawale=this.getResources().getDrawable(R.drawable.bg);
imapaper.setImageDrawable(drawale);
wpm.setBitmap(imapaper.getDrawingCache());
< uses-permission android:name =“android.permission.SET_WALLPAPER” ></ uses-permission >
14、常见通过系统服务得到的实例化对象
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
15、检查网络是否连接
代码
// 检查网络是否连接
public boolean checkIntent(){
ConnectivityManager mannager = (ConnectivityManager)
this .getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = mannager.getActiveNetworkInfo();
if (info == null || ! info.isConnected()){