mobile_numen_7
1,自定义title和Toast
title :
第一种方式:
requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main);//只需要把main的layout文件中的控件的高度设置成类似于title一样的高度.
第二种方式:
//google推荐的自定义title的做法 //1 .自定义title requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); // 注意 setFeatureInt 要写到setContentView 之后 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
Toast:
/**
* 显示一个图片和一个文本
* @param context
* @param layout
*/
public static void show(Context context, int icon, int text){
Toast toast = new Toast(context);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.mytoast, null);
TextView tv = (TextView) view.findViewById(R.id.tv_my_toast);
ImageView iv = (ImageView) view.findViewById(R.id.iv_my_toast);
tv.setText(text);
iv.setImageResource(icon);
toast.setView(view);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
2.px和dp之间的转换
public class DensityUtil { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }
3,listView和checkbox组合的时候,checkbox选择状态有问题.当我们拖动listview的条目时候,checkbox可能会乱.
所以我们需要维护一个map集合来记住listview每个item(条目)的状态.
4,关于一些android中操作程序的API
//返回设备上所有的应用程序包 pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES); //获取我们知道的关于这个指定的程序/包的所有信息 pm.getApplicationInfo(processinfo.processName,PackageManager.GET_UNINSTALLED_PACKAGES) ApplicationInfo:你能够获取到指定的应用程序信息,相当于AndroidManifest.xml的<application>标签里面信息的集合 ActivityInfo:你能够获取到关于指定的应用程序的activity获取receiver,相当于AndroidManifest.xml的<activity>和<receiver>标签里面信息的集合 关于包的内容的所有信息,相当于AndroidManifest.xml所有的信息的集合 PackageInfo info = pm.getPackageInfo(info.getPackname(), PackageManager.GET_PERMISSIONS );
返回系统中正在运行的应用程序进程, am.getRunningAppProcesses();
//获取某个pid占用的内存信息 am.getProcessMemoryInfo(new int[] { processinfo.pid }) ResolveInfo Information that is returned from resolving an intent against an IntentFilter. This partially corresponds to information collected from the AndroidManifest.xml's<intent> tags.
5,我们知道activity之间可以通过Intent.putExtra()传递参数,Activity之间的对象(自定义的复杂类型)传递可以通过application .
同时不要忘了在清单文件中配置.
6,自定义View控件
首先新建一个继承了View的类,
public class MyView extends View { Paint paint; public MyView(Context context) { super(context); Log.i("MyView", "MyView(Context context)"); paint = new Paint(); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); Log.i("MyView", "MyView(Context context, AttributeSet attrs)"); //从配置文件中读取数据 TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView); int color = array.getColor(R.styleable.MyView_text_Color, Color.BLUE); float size = array.getDimension(R.styleable.MyView_text_Size, 10); paint = new Paint(); paint.setColor(color); paint.setTextSize(size); paint.setStrokeWidth(10); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // paint.setTextSize(20); // paint.setColor(Color.RED); // paint.setStrokeWidth(10); Log.i("MyView", "onDraw"); canvas.drawText("我是被画出来的", 30, 50, paint); } }
main.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:mySP="http://schemas.android.com/apk/res/com.itheima.customise"//添加命名空间 android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.itheima.customise.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" mySP:text_Color="#ff00ff00" mySP:text_Size="60dip" > </com.itheima.customise.MyView> </LinearLayout>
在strings.xml文件中配置MyView
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">CustomiseView</string>
<!-- 会在R文件中生成 R.styleable.MyView-->
<declare-styleable name="MyView">
<!-- 会在R文件中生成 R.styleable.MyView_text_Color引用 -->
<attr format="color" name="text_Color" />
<attr format="dimension" name="text_Size" />
</declare-styleable>
</resources>