测试新版ADT与旧版的不同

谷歌发布的新版adt插件与旧版在新建工程时有不同,今天测试一下不同之处。最新版本为v22.6.2-1085508

    新版本的adt插件在新建一个工程时,会根据兼容到的最小版本号使用不同的处理,如果是低于4.0的版本,除了会自动创建工程文件外,还好主动创建一个名为appcompat_v7的工程文件作为当前工程的库工程。如果创建多个工程,这appcompat_v7这个工程会多次创建并依次appcompat_v7、appcompat_v7_2、appcompat_v7_3.....

    在新的版本中,推荐使用Fragment显示界面,由于4.0以下没有Fragment的支持所以需要使用到v4包。如果新建的工程中与库工程中v4包版本不同,则会出现冲突。可以将主工程里的v4包删除。
    
    在自动创建的MainActivity中,其引用的布局不在是线性布局或者相对布局。而是直接使用了帧布局,且默认其中无显示控件。而显示内容则搬到了MainActivity中加载的Fragment中。Fragment的布局中显示内容为旧版adt中默认显示的相对布局及其中的的HelloWorld文本。

    在MainActivity中默认加载Fragment界面,如果是低于4..0,直接使用兼容包。
package com.example.tsetlow4;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.containernew PlaceholderFragment()).commit();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }
}


而如果高于4.0则直接使用Fragment。

package com.example.edknie;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.containernew PlaceholderFragment()).commit();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }
}


默认设置都是可以更改的。只要在代码中将关于Fragment的内容删掉。就可以恢复到与旧版本相同。不过看来谷歌更推荐Fragment的使用方式!

























posted @ 2014-04-28 15:13  linwoain  阅读(439)  评论(0编辑  收藏  举报