android常用

来源:互联网 发布:ios 两个数组相同元素 编辑:程序博客网 时间:2024/06/07 17:16

如何获取分辨率:
DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;  // 屏幕宽度(像素)
        int height = metric.heightPixels;  // 屏幕高度(像素)
        float density = metric.density;  // 屏幕密度(0.75 / 1.0 / 1.5)
        int densityDpi = metric.densityDpi;  // 屏幕密度DPI(120 / 160 / 240)


densityDpi与drawable-(hdpi,mdpi,ldpi)之间的关系

系统drawable有hdpi,mdpi,ldpi三个文件夹下面存放不同尺寸的图片,使用哪个文件下的文件,与系统densityDpi值是有关系的
densityDpi=120:ldpi
densityDpi=160:mdpi
densityDpi=240:hdpi
前面我又说过densityDpi取决于显示屏,这样你就了解了为什么不同显示屏WVGA,HVGA,QVGA会采用不同drawable-(hdpi,mdpi,ldpi)图片
分辨率为240px*400px,densityDpi=120-->QVGA:ldpi
分辨率为320px*533px,densityDpi=160  -->HVGA:mdpi
分辨率为480px*800px,densityDpi=240  -->WVGA:WVGA


另外我们经常需要在程序中动态的添加一些控件,但是由于在程序中一般设置都是使用PX 因此需要借助

Resources.getDimensionPixelSize经行转换 参数为资源的Id值  这样就可以获得实际的像素值了




加载Layout布局的几种方法

   先获得LayoutInflater对象

  1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

  2.LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  3.LayoutInflater inflater = LayoutInflater.from(context);  

  使用LayoutInflater的inflate()方法加载相应的布局

  inflate()和SetContentView()的区别是:

  setContentView()一旦调用,layout就会立刻显示UI.而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)

  显示出来.一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater

  动态加载.



LoadManager是个异步加载的好东西



对于Fragment有两种方法

android.app.Fragment:兼容的最低版本是android:minSdkVersion="11"

可以使用<fragment>标签的,这点很重要,如果是用android.support.v4.app.Fragment的话,是不能是用<fragment>标签的,会抛出android.view.InflateException: Binary XML file line #7: Error inflating class fragment异常

android.support.v4.app.Fragment可以兼容到1.6的版本,不能使用<fragment>标签,需要在程序中通过add或者replace的方式添加,

同时还需要添加android-support-v4.jar

在官方文档上有这么一段话:

The main differences when using this support version instead of the framework version are:
Your activity must extend FragmentActivity
You must call getSupportFragmentManager() to get the FragmentManager

fragment是3.0以后的东西,为了在低版本中使用fragment就要用到android-support-v4.jar兼容包,而fragmentActivity就是这个兼容包里面的,它提供了操作fragment的一些方法,其功能跟3.0及以后的版本的Activity的功能一样。
下面是API中的原话:
FragmentActivity is a special activity provided in the Support Library to handle fragments on system versions older than API level 11. If the lowest system version you support is API level 11 or higher, then you can use a regular Activity.

Activity向Fragment传值:  Fragment的setArguments()传递Bundle和getArguments()得到Bundle

Fragment得到Activity中的值,用getActivity()

Activity想得到Fragment的返回值,想传递一个url,然后得到数据,我们可以通过接口回调的方式,就像button的点击事件,这样做十分灵活,可以

传递不同的参数,然后得到想要的结果

Fragment和Activity或者Fragment和Fragment之间的交流,可以通过在Fragment中创建接口,然后再Activity中实现接口,来达到访问Fragment,如下:

public static class FragmentA extends ListFragment {    ...    // Container Activity must implement this interface    public interface OnArticleSelectedListener {        public void onArticleSelected(Uri articleUri);    }    ...}

public static class FragmentA extends ListFragment {    OnArticleSelectedListener mListener;    ...    @Override    public void onAttach(Activity activity) {        super.onAttach(activity);        try {            mListener = (OnArticleSelectedListener) activity;        } catch (ClassCastException e) {            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");        }    }    ...}

public static class FragmentA extends ListFragment {    OnArticleSelectedListener mListener;    ...    @Override    public void onListItemClick(ListView l, View v, int position, long id) {        // Append the clicked item's row ID with the content provider Uri        Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);        // Send the event and Uri to the host activity        mListener.onArticleSelected(noteUri);    }    ...}

这个博客说的不错:http://blog.csdn.net/xyz_lmn/article/details/8631195

还有一个方法也不错,如果两个Fragment之间的数据访问,我们可以通过在Fragment中getFragmentManager(),然后findViewByTag()(在add

或者replace的时候第三个参数tag添上即可)来得到另外一个Fragment,然后通过getView获得Fragment的View布局,接着使用findViewById得到相应

的控件



getWidth和getMeasuredWidth的区别

getWidth得到是某个view的实际尺寸.

 getWidth在OnCreat的时候得到的是0..很奇怪, 我找到了原因: 当一个view对象创建时,android并不知道其大小,所以  getWidth()和   getHeight()返回的结果是0,真正大小是在计算布局时才会计算,所以会发现一个有趣的事,即在onDraw( ) 却  能取得长宽的原因。

getMeasuredWidth是得到某view想要在parent view里面占的大小.

 getMeasuredWidth必须在parent view或者它自己调用measure()函数之后才能得到. measure函数就是计算该函数需要占用的空间  大小.


加载XML动画资源可以使用AnimationUtils.loadAnimation()方法


按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",
如果为竖直方向的设置android:layout_height="0dp"。
在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。
 一般我们控制比重都是设置对应方向的高或宽为0dp来实现我们想的比重控制,而很少用fill_parent或wrap_content,


AsyncTask的参数我们可以自由选择,不需要的话为void即可



有时候需要在onCreate方法中知道某个View组件的宽度和高度等信息,而直接调用View组件的getWidth()、getHeight()、getMeasuredWidth()、getMeasuredHeight()、getTop()、getLeft()等方法是无法获取到真实值的,只会得到0。这是因为View组件布局要在onResume回调后完成,我们可以在onWindowFocusChanged 或者 利用ViewTreeObserver就可以办到


android:descendantFocusability   该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系



android分页常用的是   下拉加载


ListView上下滑动时变黑

手指在ListView上下滚动时,ListViewItem背景变黑,因为在滚动的时候为了提升性能做了优化,为提高滚动的性能,Android 框架在ListView中引入CacheColorHint属性。如果该值为非0,则说明该ListView绘制在单色不透明的背景上,在默认情况下该值 为#191919,也就是黑色主题中的黑色背景颜色值,这样当ListView滚动的时候就会使用该值来绘制ListView的背景。

两种解决办法:
1、xml中,ListView内新增如下属性:
Xml代码 
android:cacheColorHint="#00000000"  


 2、Java类文件中,对ListView设置如下属性:

Java代码 
listview.setCacheColorHint(Color.TRANSPARENT);


Editable 是一个接口类型,对它的实例化对象作出任何改变都是对原有的实例化对象操作的,内存地址还是原来的那个。

String 的任何改变都是相当于重新实例化了一个 String 类出来,相当于重新分配了内存地址。

所以说 Editable 是可变的,String 是不可变的了;因为 Editable 变了之后还是原来的 Editable 对象,String 变了之后就已经不是原来的 String 对象了。


drawable/
For bitmap files (PNG, JPEG, or GIF), 9-Patch image files, and XML files that describe Drawable shapes or Drawable objects that contain multiple states (normal, pressed, or focused). See the Drawable resource type.


mipmap/
For app launcher icons. The Android system retains the resources in this folder (and density-specific folders such as mipmap-xxxhdpi) regardless of the screen resolution of the device where your app is installed. This behavior allows launcher apps to pick the best resolution icon for your app to display on the home screen. For more information about using the mipmap folders, see Managing Launcher Icons as mipmap Resources.






Adapter:


由图可以看到在Android中与Adapter有关的所有接口、类的完整层级图。在我们使用过程中可以根据自己的需求实现接口或者继承类进行一定的扩展。比较常用的有 Base AdapterImpleaderAdapterCounteradaptation等。

  • BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;
  • ArrayAdapter支持泛型操作,最为简单,只能展示一行字。
  • SimpleAdapter有最好的扩充性,可以自定义出各种效果。
  • SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。
0 0
原创粉丝点击