android入门

@使用
@id/android:list,那么这个android是什么意思呢,实际上,这个android就是系统的R类(在R.java文件中)所在的package
@后面使用“+”,表示当修改完某个布局文件并保存后,系统会自动在R.java文件中生成相应的int类型变量。变量名就是“/”后面的值:@+id/xyz会在R.java文件中生成int xyz = value

Log.v(“hello”,”hello world!”);//打印标签为hello的控制台信息,其中信息输出为”hello world!”

获取当前系统时间
System.nanoTime()
System.currentTimeMillis()

com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command’/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/bin/java” finished with non-zero exit value 2
原因:
项目包含了两个相同包名的不同 project 或者 jar
Bundle类是一个key-value对
两个activity之间的通讯可以通过bundle类来实现,做法就是:
(1)新建一个bundle类
Bundle mBundle = new Bundle();
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
mBundle.putString(“Data”, “data from TestBundle”);
(3)新建一个intent对象,并将该bundle加入这个intent对象
Intent intent = new Intent();
intent.setClass(TestBundle.this, Target.class);
intent.putExtras(mBundle);
Bundle类是一个key-value对
Intent:不同activity之间传递消息
Activity:页面框架
View:页面中各种控件构成的视图
Fragment:可理解为页面的一个控件
Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带。
inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。

inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。
1.获取LayoutInflater对象有三种方法
LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);

2.关于LayoutInflater类inflate(int resource, ViewGroup root, boolean attachToRoot)方法三个参数的含义
resource:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。
root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。
attachToRoot:是否将root附加到布局文件的根视图上

Fragment 真正的完全解析
1、 Fragment的产生与介绍1、Fragment的产生与介绍
做到一个App可以同时适应手机和平板的不同尺寸需求
不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。
2、Fragment的生命周期
Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。官网这张图很好的说明了两者生命周期的关系:
可以看到Fragment比Activity多了几个额外的生命周期回调方法:
onAttach(Activity) 当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle) 创建该Fragment的视图
onActivityCreated(Bundle) 当Activity的onCreate方法返回时调用
onDestoryView() 与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach() 与onAttach相对应,当Fragment与Activity关联被取消时调用
注意:除了onCreateView,其他的所有方法如果你重写了,必须调用父类对于该方法的实现,
3、静态的使用Fragment
嘿嘿,终于到使用的时刻了~~
这是使用Fragment最简单的一种方式,把Fragment当成普通的控件,直接写在Activity的布局文件中。
1、继承Fragment,重写onCreateView决定Fragemnt的布局
2、在Activity中声明此Fragment,就当和普通的View一样
4、动态的使用Fragment
5、Fragment家族常用的API
Fragment常用的三个类:
android.app.Fragment 主要用于定义Fragment
android.app.FragmentManager 主要用于在Activity中操作Fragment
android.app.FragmentTransaction 保证一些列Fragment操作的原子性,熟悉事务这个词,一定能明白~
http://blog.csdn.net/lmj623565791/article/details/37970961

安卓兼容性:
API lever 4(即Android 1.6)或者更高版本之上。它包含了相对更多的内容,而且用的更为广泛,例如:Fragment,NotificationCompat,LoadBroadcastManager,ViewPager,PageTabStrip,Loader,FileProvider 等
Gradle引用方法:
compile ‘com.android.support:support-v4:21.0.3’
support-v7
这个包是为了考虑API level 7(即Android 2.1)及以上版本而设计的,但是v7是要依赖v4这个包的,v7支持了Action Bar以及一些Theme的兼容。
Gradle引用方法:
compile ‘com.android.support:appcompat-v7:21.0.3’
support-v13
这个包的设计是为了API level 13(即Android 3.2)及更高版本的,一般我们都不常用,平板开发中能用到,这里就不过多介绍了。
Theme
回到知乎上的这个问题,我们来介绍下各种Theme的概念。
Hoho Theme
在4.0之前Android可以说是没有设计可言的,在4.0之后推出了Android Design,从此Android在设计上有了很大的改善,而在程序实现上相应的就是Holo风格,所以你看到有类似 Theme.Holo.Light、 Theme.Holo.Light.DarkActionBar 就是4.0的设计风格,但是为了让4.0之前的版本也能有这种风格怎么办呢?这个时候就不得不引用v7包了,所以对应的就有 Theme.AppCompat.Light、 Theme.AppCompat.Light.DarkActionBar,如果你的程序最小支持的版本是4.0,那么可以不用考虑v7的兼容。
Material Design Theme
今年的5.0版本,Android推出了Material Design的概念,这是在设计上Android的又一大突破。对应的程序实现上就有 Theme.Material.Light、 Theme.Material.Light.DarkActionBar等,但是这种风格只能应用在在5.0版本的手机,如果在5.0之前应用Material Design该怎么办呢?同样的引用appcompat-v7包,这个时候的Theme.AppCompat.Light、 Theme.AppCompat.Light.DarkActionBar就是相对应兼容的Material Design的Theme。
注意事项
gradle引用appcompat-v7包的时候就不需要引用v4了,因为v7里默认包含了v4包;
compile ‘com.android.support:appcompat-v7:21.0.3’ 中的21代表API level 21推出的兼容包,所以如果你引用的是21之前的版本,则默认这些Theme.AppCompat.Light是Holo风格的,从21开始的版本默认是Material风格
使用appcompat之后,你的所有的Activity应该继承自ActionBarActivity,而ActionBarActivity继承自FragmentActivity,所以放心的使用Fragment;

注意:!!!!!!!!!!!
V7的话MainActivity继承自ActionBarActivity,使用getFragmentManager()
V4的话MainActivity继承自FragmentActivity使用getSupportFragmentManager()

Arrays.asList()
将一个数组转化为一个List对象,这个方法会返回一个ArrayList类型的对象, 这个ArrayList类并非java.util.ArrayList类,而是Arrays类的静态内部类!用这个对象对列表进行添加删除更新操作,就会 报UnsupportedOperationException异常。

可以将Intent理解为不同组件之间通信的“媒介”专门提供组件互相调用的相关信息。
两个activity之间的通讯可以通过bundle类来实现
(1)新建一个bundle类
Bundle mBundle = new Bundle();
(2)bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)
mBundle.putString(“Data”, “data from TestBundle”);
(3)
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestBundle extends Activity {

private Button button1;  
private OnClickListener cl;   
public void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.main);  

    button1 = (Button) findViewById(R.id.button1);  
    cl = new OnClickListener(){  
        @Override  
        public void onClick(View arg0) {  
            // TODO Auto-generated method stub  
            Intent intent = new Intent();    
            intent.setClass(TestBundle.this, Target.class);    
            Bundle mBundle = new Bundle();    
            mBundle.putString("Data", "data from TestBundle");//压入数据    
            intent.putExtras(mBundle);    
            startActivity(intent);  
        }  
    };  
    button1.setOnClickListener(cl);  
}  

}(4)import android.app.Activity;
import android.os.Bundle;
public class Target extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target);
Bundle bundle = getIntent().getExtras(); //得到传过来的bundle
String data = bundle.getString(“Data”);//读出数据
setTitle(data);
}
}

java.net包对统一资源定位符
(uniform resource locator URL)
和统一资源标识符
(uniform resource identifier URI)
作了非常明确的区分。
(1)URI是个纯粹的句法结构,用于指定标识Web资源的字符串的各个不同部分。URL是URI的一个特例,它包含了定位Web资源的足够信息。其他URI,比如
mailto:cay@horstman.com
则不属于定位符,因为根据该标识符无法定位任何资源。
final String FORECAST_BASE_URL =”http://api.openweathermap.org/data/2.5/forecast/daily?”;
final String QUERY_PARAM = “q”;
final String FORMAT_PARAM = “mode”;
final String UNITS_PARAM = “units”;
final String DAYS_PARAM = “cnt”;

            Uri builtUri = Uri.parse(FORECAST_BASE_URL.buildUpon()
                            .appendQueryParameter(QUERY_PARAM,params[0])
                            .appendQueryParameter(FORMAT_PARAM,format)
                            .appendQueryParameter(UNITS_PARAM,units)
                            .appendQueryParameter(DAYS_PARAM,Integer.toString(numDays))
            );

URL url = new URL(builtUri.toString());
等价于
URL url = New URL(“http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7&APPID=964db77a8bb54e9e156c8b8e0c37af23“);

Android打开各种类型的文件方法总结
public static Intent openFile(String filePath){

    File file = new File(filePath);
    if(!file.exists()) return null;
    /* 取得扩展名 */
    String end=file.getName().substring(file.getName().lastIndexOf(".") + 1,file.getName().length()).toLowerCase(); 
    /* 依扩展名的类型决定MimeType */
    if(end.equals("m4a")||end.equals("mp3")||end.equals("mid")||
            end.equals("xmf")||end.equals("ogg")||end.equals("wav")){
        return getAudioFileIntent(filePath);
    }else if(end.equals("3gp")||end.equals("mp4")){
        return getAudioFileIntent(filePath);
    }else if(end.equals("jpg")||end.equals("gif")||end.equals("png")||
            end.equals("jpeg")||end.equals("bmp")){
        return getImageFileIntent(filePath);
    }else if(end.equals("apk")){
        return getApkFileIntent(filePath);
    }else if(end.equals("ppt")){
        return getPptFileIntent(filePath);
    }else if(end.equals("xls")){
        return getExcelFileIntent(filePath);
    }else if(end.equals("doc")){
        return getWordFileIntent(filePath);
    }else if(end.equals("pdf")){
        return getPdfFileIntent(filePath);
    }else if(end.equals("chm")){
        return getChmFileIntent(filePath);
    }else if(end.equals("txt")){
        return getTextFileIntent(filePath,false);
    }else{
        return getAllIntent(filePath);
    }
}

//Android获取一个用于打开APK文件的intent
public static Intent getAllIntent( String param ) {

    Intent intent = new Intent();  
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    intent.setAction(android.content.Intent.ACTION_VIEW);  
    Uri uri = Uri.fromFile(new File(param ));
    intent.setDataAndType(uri,"*/*"); 
    return intent;
}
//Android获取一个用于打开APK文件的intent
public static Intent getApkFileIntent( String param ) {

    Intent intent = new Intent();  
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    intent.setAction(android.content.Intent.ACTION_VIEW);  
    Uri uri = Uri.fromFile(new File(param ));
    intent.setDataAndType(uri,"application/vnd.android.package-archive"); 
    return intent;
}

//Android获取一个用于打开VIDEO文件的intent
public static Intent getVideoFileIntent( String param ) {

    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("oneshot", 0);
    intent.putExtra("configchange", 0);
    Uri uri = Uri.fromFile(new File(param ));
    intent.setDataAndType(uri, "video/*");
    return intent;
}

//Android获取一个用于打开AUDIO文件的intent
public static Intent getAudioFileIntent( String param ){

    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("oneshot", 0);
    intent.putExtra("configchange", 0);
    Uri uri = Uri.fromFile(new File(param ));
    intent.setDataAndType(uri, "audio/*");
    return intent;
}

//Android获取一个用于打开Html文件的intent   
public static Intent getHtmlFileIntent( String param ){

    Uri uri = Uri.parse(param ).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();
    Intent intent = new Intent("android.intent.action.VIEW");
    intent.setDataAndType(uri, "text/html");
    return intent;
}

//Android获取一个用于打开图片文件的intent
public static Intent getImageFileIntent( String param ) {

    Intent intent = new Intent("android.intent.action.VIEW");
    intent.addCategory("android.intent.category.DEFAULT");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri uri = Uri.fromFile(new File(param ));
    intent.setDataAndType(uri, "image/*");
    return intent;
}

//Android获取一个用于打开PPT文件的intent   
public static Intent getPptFileIntent( String param ){  

    Intent intent = new Intent("android.intent.action.VIEW");   
    intent.addCategory("android.intent.category.DEFAULT");   
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
    Uri uri = Uri.fromFile(new File(param ));   
    intent.setDataAndType(uri, "application/vnd.ms-powerpoint");   
    return intent;   
}   

//Android获取一个用于打开Excel文件的intent   
public static Intent getExcelFileIntent( String param ){  

    Intent intent = new Intent("android.intent.action.VIEW");   
    intent.addCategory("android.intent.category.DEFAULT");   
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
    Uri uri = Uri.fromFile(new File(param ));   
    intent.setDataAndType(uri, "application/vnd.ms-excel");   
    return intent;   
}   

//Android获取一个用于打开Word文件的intent   
public static Intent getWordFileIntent( String param ){  

    Intent intent = new Intent("android.intent.action.VIEW");   
    intent.addCategory("android.intent.category.DEFAULT");   
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
    Uri uri = Uri.fromFile(new File(param ));   
    intent.setDataAndType(uri, "application/msword");   
    return intent;   
}   

//Android获取一个用于打开CHM文件的intent   
public static Intent getChmFileIntent( String param ){   

    Intent intent = new Intent("android.intent.action.VIEW");   
    intent.addCategory("android.intent.category.DEFAULT");   
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
    Uri uri = Uri.fromFile(new File(param ));   
    intent.setDataAndType(uri, "application/x-chm");   
    return intent;   
}   

//Android获取一个用于打开文本文件的intent   
public static Intent getTextFileIntent( String param, boolean paramBoolean){   

    Intent intent = new Intent("android.intent.action.VIEW");   
    intent.addCategory("android.intent.category.DEFAULT");   
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
    if (paramBoolean){   
        Uri uri1 = Uri.parse(param );   
        intent.setDataAndType(uri1, "text/plain");   
    }else{   
        Uri uri2 = Uri.fromFile(new File(param ));   
        intent.setDataAndType(uri2, "text/plain");   
    }   
    return intent;   
}  
//Android获取一个用于打开PDF文件的intent   
public static Intent getPdfFileIntent( String param ){   

    Intent intent = new Intent("android.intent.action.VIEW");   
    intent.addCategory("android.intent.category.DEFAULT");   
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
    Uri uri = Uri.fromFile(new File(param ));   
    intent.setDataAndType(uri, "application/pdf");   
    return intent;   

}

AsyncTask(后台处理数据传输可用此类)定义了三种泛型类型 Params,Progress和Result。

Params 启动任务执行的输入参数,比如HTTP请求的URL。
Progress 后台任务执行的百分比。
Result 后台执行任务最终返回的结果,比如String。

使用过AsyncTask 的同学都知道一个异步加载数据最少要重写以下这两个方法:

doInBackground(Params…)
后台执行,比较耗时的操作都可以放在这里。注意这里不能直接操作UI。此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间。在执行过程中可以
调用publicProgress(Progress…)来更新任务的进度。
onPostExecute(Result) 相当于Handler 处理UI的方式,在这里面可以使用在doInBackground 得到的结果处理操作UI。 此方法在主线程执行,任务执行的结果作为此方法的参数返回

有必要的话你还得重写以下这三个方法,但不是必须的:

onProgressUpdate(Progress…) 可以使用进度条增加用户体验度。 此方法在主线程执行,用于显示任务执行的进度。
onPreExecute() 这里是最终用户调用Excute时的接口,当任务执行之前开始调用此方法,可以在这里显示进度对话框。
onCancelled() 用户调用取消时,要做的操作

使用AsyncTask类,以下是几条必须遵守的准则:

Task的实例必须在UI thread中创建;
execute方法必须在UI thread中调用;
不要手动的调用onPreExecute(), onPostExecute(Result),doInBackground(Params…), onProgressUpdate(Progress…)这几个方法;
该task只能被执行一次,否则多次调用时将会出现异常;

ContentProvider
1、ContentProvider的基本概念
1> ContentProvider为存储和读取数据提供了统一的接口
2> 使用ContentProvider,应用程序可以实现数据共享
3> android内置的许多数据都是使用ContentProvider形式,供开发者调用的(如视频,音频,图片,通讯录等)
2、Uri
1> 每一个ContentProvider都拥有一个公共的Uri,这个Uri用于表示这个ContentProvider提供的数据
2> Android所提供的ContentProvider都存放在andriod.provider这个包里面
3、ContentProvider提供的函数
1> query() 查询
2> insert() 插入
3> update() 更新
4> delete() 删除
  5> getType() 得到数据类型
6> onCreate() 创建时的回调函数
4、实现ContentProvider的过程
1> 定义一个COTENT_URI常量
2> 定义一个类,继承ContentProvider
3> 实现query(),delete(),update(),insert(),onCreate(),getType()方法
4> 在AndroidMainfest.xml中申明

三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便。
android布局–Android fill_parent、wrap_content和match_parent的区别
1)fill_parent
设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。这跟Windows控件的dockstyle属性大体一致。设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。
2) wrap_content
设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。
3)match_parent
Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了

ArrayAdapter(适配器)的创建方法
API:
ArrayAdapter myArrayAdapter =
new ArrayAdapter(this, android.layout.simple_list_item_1, myArrayList);

默认的,ArrayAdapter期望接受的样式文件里只含有一个textview,然后它把接受到的数据toString后(即调用数据对象的toString方法)展示在textview里。

一、简单的。

这样的列表的每一行都只有一行文字。

// 当然listview 也可以是在layout里写好,然后findViewById()获取出来,这样的话后面就不需setContentView(listview);      
        ListView listview = new ListView(this);  
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,Android.R.layout.simple_expandable_list_item_1);  
        adapter.add("string1");  
        adapter.add("haha");  
        adapter.add("heihei");        
        listview.setAdapter(adapter);  
        setContentView(listview); 

上面代码中,android.R.layout.simple_expandable_list_item_1是android里已提供的样式,我们也可换成自己的xml。但是需要注意的是这个xml文件仅能有一个textview。连Layout也不能有。否则会报错:ArrayAdapter requires the resource ID to be a TextView

如layout下有online_user_list_item.xml,它的内容如下:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="wrap_content"   
android:layout_height="wrap_content"    
android:id="@+id/online_user_list_item_textview" >  
</TextView>    

则android.R.layout.simple_expandable_list_item_1换成R.layout.online_user_list_item。

如果我们想要使用更复杂一点的layout,而不仅是只有一个textview,那就要用下面这种。

二、样式丰富但内容简单的。

layout下的online_user_list_item.xml内容如下:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content">  
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"  android:id="@+id/online_user_list_item_textview" android:text="TextView"></TextView>  
<Button  
    android:text="button"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content">  
</Button>  
</LinearLayout>  

里面含有的textview是我们想要展示内容的地方。那么构建ArrayAdapter时,应该这样写:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.online_user_list_item, R.id.online_user_list_item_textview);   

如果我们需要展示的内容是一仅一个textview承载不了的,还需要其它组件,怎么办?我们可以自定义。

三、内容丰富的(自定义ArrayAdapter)。

这就需要写一个类继承自ArrayAdapter并且重写getView方法。上代码:

public class UserListAdapter extends ArrayAdapter<User> {  
    private int resourceId;  
    public UserListAdapter(Context context, int textViewResourceId, List<User> objects) {  
        super(context, textViewResourceId, objects);  
        this.resourceId = textViewResourceId;  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent){  
        User user = getItem(position);  
        LinearLayout userListItem = new LinearLayout(getContext());  
        String inflater = Context.LAYOUT_INFLATER_SERVICE;   
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);   
        vi.inflate(resourceId, userListItem, true);  
        TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username);  
        TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum);  
        TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg);  
        tvUsername.setText(user.getUsername());  
        tvAskedNum.setText(String.valueOf(user.getAskedNum()));  
        tvLastMsg.setText(user.getLastMsg());  
        return userListItem;  
    }  
}  

activity里就这样写

List<User> users = new ArrayList<User>();  
        User user = new User();  
        user.setAskedNum(8);  
        user.setLastMsg("hello");  
        user.setUsername("pxx");  
        users.add(user);  
        users.add(user);  
        users.add(user);  
        UserListAdapter adapter = new UserListAdapter(this,R.layout.online_user_list_item,users);  
        listview.setAdapter(adapter);  

常用api:
Button b =(Button)this.findViewById(R.id.btn);//在视图中找到id为btn的控件并传给b

LayoutInflater的inflate函数用法详解
LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

获取LayoutInflater的方法有如下三种:
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);

LayoutInflater inflater = LayoutInflater.from(context); (该方法实质就是第一种方法,可参考源代码)
View layout = inflater.inflate(R.layout.main, null);

LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)
View layout = inflater.inflate(R.layout.main, null);
一直有点纠结setContentView和inflate的区别找了一些资料。写了个小程序看了下:
public class MyInflate extends Activity{
private TextView tv;
public void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
//tv = (TextView) findViewById(R.id.tv);

    LayoutInflater inflate = LayoutInflater.from(this);
    View view = inflate.inflate(R.layout.main,null);
    setContentView(view);
}

}
上述注释掉的代码和没有注释掉的代码两种情况是相同的。

区别:
setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来。一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater动态加载。

public View inflate(int Resourece,ViewGroup root)
作用:填充一个新的视图层次结构从指定的XML资源文件中
reSource:View的layout的ID
root: 生成的层次结构的根视图
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。

其余几个重载的inflate函数类似。

android平台定义的主题样式
Android平台定义的主题样式:

android:theme=”@android:style/Theme.Dialog” 将一个Activity显示为对话框模式
•android:theme=”@android:style/Theme.NoTitleBar” 不显示应用程序标题栏
•android:theme=”@android:style/Theme.NoTitleBar.Fullscreen” 不显示应用程序标题栏,并全屏
•android:theme=”@android:style/Theme.Light” 背景为白色
•android:theme=”@android:style/Theme.Light.NoTitleBar” 白色背景并无标题栏
•android:theme=”@android:style/Theme.Light.NoTitleBar.Fullscreen” 白色背景,无标题栏,全屏
•android:theme=”@android:style/Theme.Black” 背景黑色
•android:theme=”@android:style/Theme.Black.NoTitleBar” 黑色背景并无标题栏
•android:theme=”@android:style/Theme.Black.NoTitleBar.Fullscreen” 黑色背景,无标题栏,全屏
•android:theme=”@android:style/Theme.Wallpaper” 用系统桌面为应用程序背景
•android:theme=”@android:style/Theme.Wallpaper.NoTitleBar” 用系统桌面为应用程序背景,且无标题栏
•android:theme=”@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen” 用系统桌面为应用程序背景,无标题栏,全屏
•android:theme=”@android:style/Translucent” 半透明效果
•android:theme=”@android:style/Theme.Translucent.NoTitleBar” 半透明并无标题栏
•android:theme=”@android:style/Theme.Translucent.NoTitleBar.Fullscreen” 半透明效果,无标题栏,全屏
•android:theme=”@android:style/Theme.Panel”

Android平台定义了三种字体大小:

“?android:attr/textAppearanceLarge”
“?android:attr/textAppearanceMedium”
“?android:attr/textAppearanceSmall”

Android字体颜色:

android:textColor=”?android:attr/textColorPrimary”
android:textColor=”?android:attr/textColorSecondary”
android:textColor=”?android:attr/textColorTertiary”
android:textColor=”?android:attr/textColorPrimaryInverse”
android:textColor=”?android:attr/textColorSecondaryInverse”

Android的ProgressBar样式:

style=”?android:attr/progressBarStyleHorizontal”
style=”?android:attr/progressBarStyleLarge”
style=”?android:attr/progressBarStyleSmall”
style=”?android:attr/progressBarStyleSmallTitle”

分隔符

横向: android:layout_width=”fill_parent”
android:layout_height=”1dip”
android:background=”?android:attr/listDivider” />
纵向:
android:layout_height=”fill_parent”
android:background=”?android:attr/listDivider” /

CheckBox样式

style=”?android:attr/starStyle”

类似标题栏效果的TextView
style=”?android:attr/listSeparatorTextViewStyle”

其它有用的样式
android:layout_height=”?android:attr/listPreferredItemHeight”
android:paddingRight=”?android:attr/scrollbarSize”
style=”?android:attr/windowTitleBackgroundStyle”
style=”?android:attr/windowTitleStyle”
android:layout_height=”?android:attr/windowTitleSize”
android:background=”?android:attr/windowBackground”

修改Activity的标题栏样式

如在styles.xml中增加
<resources>
<style name=”AutoWindowTitleBackground”>
<item name=”android:background”>#778899

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值