一些经验(三)

Thread实现定时器

new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while(true){ try{ System.out.println("sleep 1000ms"); Thread.sleep(1000); }catch(Exception e){ e.printStackTrace(); } } } }).start();

Timer实现定时器

Timer tm = new Timer(); class myTimerTask extends TimerTask{ @Override public void run() { // TODO Auto-generated method stub System.out.println("timer 1000ms"); } } tm.schedule(new myTimerTask(), 0, 1000);

Broadcast

//注册自定义BroadcastReceiver(1) this.registerReceiver(CameraTakenReceiver, new IntentFilter("com.android.camera.NEW_PICTURE")); Intent intent = new Intent("com.android.camera.NEW_PICTURE"); this.sendBroadcast(intent); /**********************************************************/ private BroadcastReceiver CameraTakenReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) {} };

横屏启动activity

方法1:在androidmanyfest.xmlactivity中加入属性 android:screenOrientation="landscape"
方法2:在oncreate中加入如下代码
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
避免在转屏时重启activity
要避免在转屏时重启activity,可以通过在androidmanifest.xml文件中重新定义方向(给每个activity加上android:configChanges=”keyboardHidden|orientation属性),并根据Activity的重写onConfigurationChanged(Configuration newConfig)方法来控制,这样在转屏时就不会重启activity了,而是会去调用onConfigurationChanged(Configuration newConfig)这个钩子方法。例如:
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){ //横向 setContentView(R.layout.file_list_landscape); }else{ //竖向 setContentView(R.layout.file_list_portrait); }
在模拟器中,要使程序转屏可以使用快捷键F12Ctrl+F11来切换。当然在用命令行启动模拟器时可以直接使用参数emulator.exe -skin HVGA-L来启动横屏的程序。
设置Activity为全屏
在AndroidManifest.xml的Activity声明中加入属性 : android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
也可以在onCreate方法里,调用
//窗口去掉标题 requestWindowFeature(Window.FEATURE_NO_TITLE); //窗口设置为全屏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置窗口为半透明 getWindow().setFormat(PixelFormat.TRANSLUCENT);   
xml中的资源引用——@[package:]type/name
注意这里 '@' 字首的使用来引入一个资源引用 -- 紧跟的文本是一个处于
@[package:]type/name 结构中的资源名称。要引用一个系统资源,你应该需 要
编写:
< EditText android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:textColor = "@color/opaque_red" android:text = "Hello, World!" /> < EditText android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:textColor = "@android:color/background_light" android:text = "Hello, World!" />
Android预定义样式
字体大小 对于能够显示文字的控件(如TextView EditText RadioButton Button CheckBox Chronometer等等),你有时需要控制字体的大小。Android平台定义了三种字体大小。 "?android:attr/textAppearanceLarge" "?android:attr/textAppearanceMedium" "?android:attr/textAppearanceSmall" 使用方法为: android:textAppearance="?android:attr/textAppearanceLarge" android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceSmall" 或 style="?android:attr/textAppearanceLarge" style="?android:attr/textAppearanceMedium" style="?android:attr/textAppearanceSmall" 字体颜色 android:textColor="?android:attr/textColorPrimary" android:textColor="?android:attr/textColorSecondary" android:textColor="?android:attr/textColorTertiary" android:textColor="?android:attr/textColorPrimaryInverse" android:textColor="?android:attr/textColorSecondaryInverse" ProgressBar style="?android:attr/progressBarStyleHorizontal" style="?android:attr/progressBarStyleLarge" style="?android:attr/progressBarStyleSmall" style="?android:attr/progressBarStyleSmallTitle" 分隔符 横向: <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider" /> 纵向: <View android:layout_width="1dip" android:layout_height="fill_parent" android:background="?android:attr/listDivider" /> CheckBox style="?android:attr/starStyle" 类似标题栏效果的TextView style="?android:attr/listSeparatorTextViewStyle" 其它有用的样式 android:layout_height="?android:attr/listPreferredItemHeight" android:paddingRight="?android:attr/scrollbarSize" style="?android:attr/windowTitleBackgroundStyle" style="?android:attr/windowTitleStyle" android:layout_height="?android:attr/windowTitleSize" android:background="?android:attr/windowBackground" 更多细节可参考SDK文档的R.attr类。

你可能感兴趣的:(一些经验(三))