一:Android 滚动条常见问题
1:子元素不能多余一个
2:不显示底部或者顶部的阴影
android:fadingEdge="none"
二:后台设置颜色
Color.parseColor("#91d439")
三:dp,px之间的转化
- public float Dp2Px(Context context, float dp) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (float) (dp * scale + 0.5f);
- }
四:弹出对话框
- AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
- //builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("选择船舶");
- // 指定下拉列表的显示数据
- final String[] cities = {"广州", "上海", "北京", "香港", "澳门"};
- // 设置一个下拉的列表选择项
- builder.setItems(cities, new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- tv.setText("选择船舶:"+cities[which]);
- System.out.println("选择的城市为:" + cities[which]);
- }
- });
- builder.show();
五: android application与activity上边都可以设置Label
可以在图标与不同页面显示Label
六:Activity动画切换
http://my.oschina.NET/u/556624/blog/102552
http://my.oschina.net/u/556624/blog/102552
七:Android实现再按一次退出程序
- private long exitTime = 0;
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
- if((System.currentTimeMillis()-exitTime) > 2000){
- Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
- exitTime = System.currentTimeMillis();
- } else {
- finish();
- System.exit(0);
- }
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
八:Android实现跨越多个activity跳转
例如在设置页面实现退出登录,如果直接finish()回退出到主界面,无法跳转到登录页
- Intent intent = new Intent();
- //跨越多个activity,删除回退栈的所有activity
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intent.setClass(SetingActivity.this, LoginActivity.class);
- startActivity(intent);
一:实现同心圆加小三角指向效果
同心圆
- public class RingView extends View{
- private Paint paint;
- private Context context;
- private int radius;//半径
- private int color;//颜色值
- public RingView(Context context) {
- // TODO Auto-generated constructor stub
- this(context, null);
- }
- public RingView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- this.context = context;
- this.paint = new Paint();
- this.paint.setAntiAlias(true); //消除锯齿
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- int center = getWidth()/2;
- //绘制内圆
- paint.setStyle(Paint.Style.FILL);
- paint.setColor(Color.parseColor("#91d439"));
- canvas.drawCircle(center,center, 22, this.paint);
- //绘制外圆(空心)
- paint.setStyle(Paint.Style.STROKE);
- paint.setColor(Color.parseColor("#91d439"));
- paint.setStrokeWidth(6);
- canvas.drawCircle(center,center, 30, this.paint);
- super.onDraw(canvas);
- }
- public int getRadius() {
- return radius;
- }
- public void setRadius(int radius) {
- this.radius = radius;
- }
- public int getColor() {
- return color;
- }
- public void setColor(int color) {
- this.color = color;
- }
- }
这里的getWidth可以根据控件设置的宽度来得到
小三角
- public class mytriangle extends View{
- private Paint paint;
- private Context context;
- private int radius;//半径
- private int color;//颜色值
- public mytriangle(Context context) {
- // TODO Auto-generated constructor stub
- this(context, null);
- }
- public mytriangle(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- this.context = context;
- this.paint = new Paint();
- this.paint.setAntiAlias(true); //消除锯齿
- }
- @Override
- protected void onDraw(Canvas canvas) {
- float x= getX();//得到控件的所在位置
- float y= getX();
- paint.setStyle(Paint.Style.FILL);
- paint.setColor(Color.parseColor("#b9dc8c"));
- // canvas.drawText("画三角形:", 10, 200, paint);
- // 绘制这个三角形,你可以绘制任意多边形
- Path path = new Path();
- // y=(float) (y+15.5);
- path.moveTo(Dp2Px(context,x+10),Dp2Px(context,y));// 此点为多边形的起点
- path.lineTo(Dp2Px(context,x), Dp2Px(context,y+7));
- path.lineTo(Dp2Px(context,x+10), Dp2Px(context,y+14));
- path.close(); // 使这些点构成封闭的多边形
- canvas.drawPath(path,paint);
- super.onDraw(canvas);
- }
- public float Dp2Px(Context context, float dp) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (float) (dp * scale + 0.5f);
- }
- public int getRadius() {
- return radius;
- }
- public void setRadius(int radius) {
- this.radius = radius;
- }
- public int getColor() {
- return color;
- }
- public void setColor(int color) {
- this.color = color;
- }
- }
布局文件中使用用全类名访问就可以了,还可以设置自动义属性,
getx(),gety得到控件的所在的坐标位置
int[] location = new int[2] ;
getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标包括通知栏
System.out.println("OnScreen x:"+location[0]+" y:"+location[1]);
注意:不要以为获取到了这个位置设置下就可以让控件在具体的位置了,而是所在布局文件类型以,0,0开始,所有
不要去设置位置默认就在那个位置,设置了反而让控件不在本身正确的位置了
- <com.lc.mycontrol.RingView
- android:layout_width="100dp"
- android:layout_height="100dp"
- />
http://www.it165.net/pro/html/201409/22252.html
http://www.it165.net/pro/html/201406/15815.html
二:画虚线
- <com.lc.mycontrol.DashedLine
- android:id="@+id/dashedLine"
- android:layout_width="wrap_content"
- android:layout_marginLeft="50dp"
- android:layout_height="300dp"
- />
- public class DashedLine extends View{
- private final String namespace = "http://com.smartmap.driverbook";
- private float startX;
- private float startY;
- private float endX;
- private float endY;
- private Rect mRect;
- Context contextgb;
- public DashedLine(Context context, AttributeSet attrs) {
- super(context, attrs);
- contextgb = context;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- int[] location = new int[2] ;
- //this.getLocationInWindow(location); //获取在当前窗口内的绝对坐标
- //System.out.println("InWindow x:"+location[0]+" y:"+location[1]);
- this.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
- System.out.println("OnScreen x:"+location[0]+" y:"+location[1]);
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- Paint paint = new Paint();
- paint.setStyle(Paint.Style.STROKE);
- paint.setColor(Color.WHITE);
- Path path = new Path();
- //location[0]获取到控件所在的x坐标,可以设置改控件位置
- path.moveTo(5, Dp2Px(contextgb,10));
- path.lineTo(5, Dp2Px(contextgb,56));
- PathEffect effects = new DashPathEffect(new float[]{2,2,2,2},2);
- paint.setPathEffect(effects);
- paint.setStrokeWidth((float) 1.5);
- canvas.drawPath(path, paint);
- }
- public float Dp2Px(Context context, float dp) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (float) (dp * scale + 0.5f);
- }
- }
一:实现同心圆加小三角指向效果
同心圆
- public class RingView extends View{
- private Paint paint;
- private Context context;
- private int radius;//半径
- private int color;//颜色值
- public RingView(Context context) {
- // TODO Auto-generated constructor stub
- this(context, null);
- }
- public RingView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- this.context = context;
- this.paint = new Paint();
- this.paint.setAntiAlias(true); //消除锯齿
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- int center = getWidth()/2;
- //绘制内圆
- paint.setStyle(Paint.Style.FILL);
- paint.setColor(Color.parseColor("#91d439"));
- canvas.drawCircle(center,center, 22, this.paint);
- //绘制外圆(空心)
- paint.setStyle(Paint.Style.STROKE);
- paint.setColor(Color.parseColor("#91d439"));
- paint.setStrokeWidth(6);
- canvas.drawCircle(center,center, 30, this.paint);
- super.onDraw(canvas);
- }
- public int getRadius() {
- return radius;
- }
- public void setRadius(int radius) {
- this.radius = radius;
- }
- public int getColor() {
- return color;
- }
- public void setColor(int color) {
- this.color = color;
- }
- }
这里的getWidth可以根据控件设置的宽度来得到
小三角
- public class mytriangle extends View{
- private Paint paint;
- private Context context;
- private int radius;//半径
- private int color;//颜色值
- public mytriangle(Context context) {
- // TODO Auto-generated constructor stub
- this(context, null);
- }
- public mytriangle(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- this.context = context;
- this.paint = new Paint();
- this.paint.setAntiAlias(true); //消除锯齿
- }
- @Override
- protected void onDraw(Canvas canvas) {
- float x= getX();//得到控件的所在位置
- float y= getX();
- paint.setStyle(Paint.Style.FILL);
- paint.setColor(Color.parseColor("#b9dc8c"));
- // canvas.drawText("画三角形:", 10, 200, paint);
- // 绘制这个三角形,你可以绘制任意多边形
- Path path = new Path();
- // y=(float) (y+15.5);
- path.moveTo(Dp2Px(context,x+10),Dp2Px(context,y));// 此点为多边形的起点
- path.lineTo(Dp2Px(context,x), Dp2Px(context,y+7));
- path.lineTo(Dp2Px(context,x+10), Dp2Px(context,y+14));
- path.close(); // 使这些点构成封闭的多边形
- canvas.drawPath(path,paint);
- super.onDraw(canvas);
- }
- public float Dp2Px(Context context, float dp) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (float) (dp * scale + 0.5f);
- }
- public int getRadius() {
- return radius;
- }
- public void setRadius(int radius) {
- this.radius = radius;
- }
- public int getColor() {
- return color;
- }
- public void setColor(int color) {
- this.color = color;
- }
- }
布局文件中使用用全类名访问就可以了,还可以设置自动义属性,
getx(),gety得到控件的所在的坐标位置
int[] location = new int[2] ;
getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标包括通知栏
System.out.println("OnScreen x:"+location[0]+" y:"+location[1]);
注意:不要以为获取到了这个位置设置下就可以让控件在具体的位置了,而是所在布局文件类型以,0,0开始,所有
不要去设置位置默认就在那个位置,设置了反而让控件不在本身正确的位置了
- <com.lc.mycontrol.RingView
- android:layout_width="100dp"
- android:layout_height="100dp"
- />
http://www.it165.net/pro/html/201409/22252.html
http://www.it165.net/pro/html/201406/15815.html
二:画虚线
- <com.lc.mycontrol.DashedLine
- android:id="@+id/dashedLine"
- android:layout_width="wrap_content"
- android:layout_marginLeft="50dp"
- android:layout_height="300dp"
- />
- public class DashedLine extends View{
- private final String namespace = "http://com.smartmap.driverbook";
- private float startX;
- private float startY;
- private float endX;
- private float endY;
- private Rect mRect;
- Context contextgb;
- public DashedLine(Context context, AttributeSet attrs) {
- super(context, attrs);
- contextgb = context;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- int[] location = new int[2] ;
- //this.getLocationInWindow(location); //获取在当前窗口内的绝对坐标
- //System.out.println("InWindow x:"+location[0]+" y:"+location[1]);
- this.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
- System.out.println("OnScreen x:"+location[0]+" y:"+location[1]);
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- Paint paint = new Paint();
- paint.setStyle(Paint.Style.STROKE);
- paint.setColor(Color.WHITE);
- Path path = new Path();
- //location[0]获取到控件所在的x坐标,可以设置改控件位置
- path.moveTo(5, Dp2Px(contextgb,10));
- path.lineTo(5, Dp2Px(contextgb,56));
- PathEffect effects = new DashPathEffect(new float[]{2,2,2,2},2);
- paint.setPathEffect(effects);
- paint.setStrokeWidth((float) 1.5);
- canvas.drawPath(path, paint);
- }
- public float Dp2Px(Context context, float dp) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (float) (dp * scale + 0.5f);
- }
- }