Android常用知識筆記

來源:互聯網
上載者:User

標籤:

 

1、 安卓圖片自適應

android從1.6和更高,Google為了方便開發人員對於各種解析度機型的移植而增加了自動適配的功能 
<supports-screens 
android:largeScreens="true" 
android:normalScreens="true" 
android:smallScreens="true" 
android:anyDensity="true"/> 

2、 /*擷取螢幕像素和螢幕密度*/

       DisplayMetrics metrics=new DisplayMetrics();

       getWindowManager().getDefaultDisplay().getMetrics(metrics);

       int width=metrics.widthPixels;

       int height=metrics.heightPixels;

       float density=metrics.density;

        int densityDpi=metrics.densityDpi;

3、  Android2.2中match_parent和fill_parent是一個意思 .兩個參數意思一樣,match_parent更貼切,於是從2.2開始兩個詞都可以用。那麼如果考慮低版本的使用方式你就需要用fill_parent了

4、  讀取電話薄

AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

java檔案:

public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       //setContentView(R.layout.activity_contacts);

       TextView tv = new TextView(this);

       // TextView tv =(TextView) findViewById(R.id.textViewC1);

       String string = "連絡人如下:\n";

       try {

           // 得到ContentResolver對象

           ContentResolver contentResolver = getContentResolver();

           // 取得電話本中開始一項的游標

           Cursor cursor = contentResolver.query(

                  ContactsContract.Contacts.CONTENT_URI, null, null, null,

                  null);

           // 向下移動游標

           while (cursor.moveToNext()) {

              // 取得連絡人名字

              int nameFieldColumnIndex = cursor

                     .getColumnIndex(PhoneLookup.DISPLAY_NAME);

              String contract = cursor.getString(nameFieldColumnIndex);

              // 取得電話號碼

             

               intnumberFieldColumnIndex = cursor

               .getColumnIndex(PhoneLookup.NUMBER); String number =

               cursor.getString(numberFieldColumnIndex);

              string += contract + "\n";

           }

           cursor.close();

           tv.setText(string);

           setContentView(tv);

       } catch (Exception e) {

           e.printStackTrace();

           Log.i(TAG, e.toString());

       }

    }

5、視窗切換public void onCreate(BundlesavedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_01);

       Log.i(TAG, "onCreate");

       Button button1 = (Button) findViewById(R.id.button11);

       /* 監聽button的事件資訊 */

button1.setOnClickListener(new Button.OnClickListener() {

           public void onClick(View v) {

              // 建立一個Intent對象

              Intent intent = new Intent();

              // 指定Intent要啟動的類

              intent.setClass(Activity01.this, Activity02.class);

              // 啟動一個新的Activity

              startActivity(intent);

              // 關閉當前的Activity

              Activity01.this.finish();

           }

       });

}

6、  android.intent.action.MAIN決定應用程式最先啟動的Activity ,android.intent.category.LAUNCHER決定應用程式是否顯示在程式列表裡。Main和LAUNCHER同時設定才有意義

Android常用知識筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.