Android 笔记一

Android UI与应用管理

1、findViewById

android 的用户界面一般使用xml文件做的,对应的xml文件在layout包下

如果xml里放了个按钮什么的,在activity中要获取该按钮就用

findViewById(R.id.xml文件中对应的id)

2ScrollView

它可以使用用户滚动显示一个占据的空间大于物理显示的视图列表。值得注意的是,ScrollView只能包含一个子视图或视图组,在实际项目中,通常包含的是一个垂直的LinearLayout。

值得注意的是,ScrollView不能和ListView一起使用,因为ListView已经对垂直方向的滚动做了处理,它会迫使如果ListView的内容大于物理视图的内容的时候,强制垂直滚动的效果,所以这里使用ScrollView和ListView混合使用是没有意义的

在Android平台下,与ScrollView类似的还有一个HorizontalScrollView容器,这个容器与ScrollView的作用相反,主要适用于水平滚动,了解了ScrollView就基本上了解了HorizontalScrollView

布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
 2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <LinearLayout
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:orientation="vertical" >
10 
11         <TextView
12             android:layout_width="match_parent"
13             android:layout_height="wrap_content"
14             android:text="垂直滚动视图"
15             android:textSize="30dp" />
16 
17         <ImageView
18             android:layout_width="match_parent"
19             android:layout_height="wrap_content"
20             android:src="@drawable/bmp1" />
21 
22         <ImageView
23             android:layout_width="match_parent"
24             android:layout_height="wrap_content"
25             android:src="@drawable/bmp2" />
26 
27         <ImageView
28             android:layout_width="match_parent"
29             android:layout_height="wrap_content"
30             android:src="@drawable/bmp3" />
31 
32         <EditText
33             android:maxLines="2"
34             android:layout_width="match_parent"
35             android:layout_height="40dp" />
36 
37         <ImageView
38             android:layout_width="match_parent"
39             android:layout_height="wrap_content"
40             android:src="@drawable/bmp4" />
41 
42         <ImageView
43             android:layout_width="match_parent"
44             android:layout_height="wrap_content"
45             android:src="@drawable/bmp5" />
46 
47         <ImageView
48             android:layout_width="match_parent"
49             android:layout_height="wrap_content"
50             android:src="@drawable/bmp6" />
51 
52         <ImageView
53             android:layout_width="match_parent"
54             android:layout_height="wrap_content"
55             android:src="@drawable/bmp7" />
56 
57         <ImageView
58             android:layout_width="match_parent"
59             android:layout_height="wrap_content"
60             android:src="@drawable/bmp8" />
61 
62         <ImageView
63             android:layout_width="match_parent"
64             android:layout_height="wrap_content"
65             android:src="@drawable/bmp9" />
66 
67         <ImageView
68             android:layout_width="match_parent"
69             android:layout_height="wrap_content"
70             android:src="@drawable/bmp10" />
71     </LinearLayout>
72 
73 </ScrollView>

3

PackageManger

说明: 获得已安装的应用程序信息 。可以通过getPackageManager()方法获得。

常用方法:

public abstract PackageManager getPackageManager()   功能:获得一个PackageManger对象  

public abstract Drawable getApplicationIcon(String packageName) 参数: packageName 包名 功能:返回给定包名的图标,否则返回null

public abstract ApplicationInfo   getApplicationInfo(String packageName, int flags) 参数:

  packagename 包名   flags 该ApplicationInfo是此flags标记,通常可以直接赋予常数0即可 功能:返回该ApplicationInfo对象

public abstract List<ApplicationInfo>  getInstalledApplications(int flags) 参数:

  flag为一般为GET_UNINSTALLED_PACKAGES,那么此时会返回所有ApplicationInfo。我们可以对ApplicationInfo   的flags过滤,得到我们需要的。 功能:返回给定条件的所有PackageInfo

public abstract List<PackageInfo>  getInstalledPackages(int flags)  参数如上 功能:返回给定条件的所有PackageInfo

public abstractResolveInfo  resolveActivity(Intent intent, int flags) 参数: 

  intent 查寻条件,Activity所配置的action和category   flags: MATCH_DEFAULT_ONLY    :Category必须带有CATEGORY_DEFAULT的Activity,才匹配   GET_INTENT_FILTERS         :匹配Intent条件即可   GET_RESOLVED_FILTER    :匹配Intent条件即可 功能 :返回给定条件的ResolveInfo对象(本质上是Activity)

public abstract  List<ResolveInfo>  queryIntentActivities(Intent intent, int flags) 参数同上 功能 :返回给定条件的所有ResolveInfo对象(本质上是Activity),集合对象  

public abstract ResolveInfo  resolveService(Intent intent, int flags) 参数同上 功能 :返回给定条件的ResolveInfo对象(本质上是Service)  

public abstract List<ResolveInfo> queryIntentServices(Intent intent, int flags) 参数同上 功能 :返回给定条件的所有ResolveInfo对象(本质上是Service),集合对象

4. hasSystemFeature()

getPackageManager().hasSystemFeature(String string).通过该函数判断系统是否有特定的模块功能。

 例如判断是否有 wifi 和 蓝牙模块的具体代码:

getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);

getPackageManager().hasSystemFe(PackageManager.FEATURE_BLUETOOTH);

5Toast.makeText

Toast 是一个 View 视图,快速的为用户显示少量的信息。 Toast 在应用程序上浮动显示信息给用户,它永远不会获得焦点,不影响用户的输入等操作,主要用于 一些帮助 / 提示。

Toast 最常见的创建方式是使用静态方法 Toast.makeText

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// 第一个参数:当前的上下文环境。可用getApplicationContext()或this

// 第二个参数:要显示的字符串。也可是R.string中字符串ID

// 第三个参数:显示的时间长短。Toast默认的有两个LENGTH_LONG(长)和LENGTH_SHORT(短),也可以使用毫秒如2000ms

Toast toast=Toast.makeText(getApplicationContext(), "默认的Toast", Toast.LENGTH_SHORT);

//显示toast信息

toast.show();

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 Toast toast=Toast.makeText(getApplicationContext(), "自定义显示位置的Toast", Toast.LENGTH_SHORT);

2 //第一个参数:设置toast在屏幕中显示的位置。我现在的设置是居中靠顶

3 //第二个参数:相对于第一个参数设置toast位置的横向X轴的偏移量,正数向右偏移,负数向左偏移

4 //第三个参数:同的第二个参数道理一样

5 //如果你设置的偏移量超过了屏幕的范围,toast将在屏幕内靠近超出的那个边界显示

6 toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);

7 //屏幕居中显示,X轴和Y轴偏移量都是0

8 //toast.setGravity(Gravity.CENTER, 0, 0);

9 toast.show();

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1 Toast toast=Toast.makeText(getApplicationContext(), "显示带图片的toast", 3000);   

2 toast.setGravity(Gravity.CENTER, 0, 0);   

3 //创建图片视图对象  

4 ImageView imageView= new ImageView(getApplicationContext());   5 //设置图片  

6 imageView.setImageResource(R.drawable.ic_launcher);   

7 //获得toast的布局  

8 LinearLayout toastView = (LinearLayout) toast.getView();   

9 //设置此布局为横向的

10 toastView.setOrientation(LinearLayout.HORIZONTAL); 

11 //将ImageView在加入到此布局中的第一个位置

12 toastView.addView(imageView, 0); 

13 toast.show();

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------

1 //Inflater意思是充气

2 //LayoutInflater这个类用来实例化XML文件到其相应的视图对象的布局  

3 LayoutInflater inflater = getLayoutInflater();   

4 //通过制定XML文件及布局ID来填充一个视图对象  

5 View layout = inflater.inflate(R.layout.custom2,(ViewGroup)findViewById(R.id.llToast));   

6   

7 ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);   

8 //设置布局中图片视图中图片  

9 image.setImageResource(R.drawable.ic_launcher); 

10 

11 TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); 

12 //设置标题

13 title.setText("标题栏"); 

14 

15 TextView text = (TextView) layout.findViewById(R.id.tvTextToast); 

16 //设置内容

17 text.setText("完全自定义Toast"); 

18 

19 Toast toast= new Toast(getApplicationContext()); 

20 toast.setGravity(Gravity.CENTER , 0, 0); 

21 toast.setDuration(Toast.LENGTH_LONG); 

22 toast.setView(layout); 

23 toast.show();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值