第一天
1:ExpandableListView,二级列表
用的ExpandableAdapter适配器。2:ImageSwitcher实现滑动效果
(1) 创建两个ImageView,给ImageSwitcher
(2)通过ViewFactory来实现ImageSwitcher
3:加入动画
TextSwitcher,文本的切换
4:ViewFlipper:
多个页面的切换
5: (1)Options menu and action bar 选项菜单
方法一:
第一步:在自动生成的menu.main.xml中:
<item
android:id="@+id/game_set"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="游戏设置"
android:icon="@android:drawable/ic_menu_set_as"
/>
<item
android:id="@+id/game_start"
android:orderInCategory="200"
android:showAsAction="ifRoom"
android:title="开始游戏"
android:icon="@android:drawable/ic_media_play"
/>
<item
android:id="@+id/game_end"
android:orderInCategory="300"
android:showAsAction="never|always"
android:title="退出游戏"
android:icon="@android:drawable/ic_delete"
/>
第二步:
//程序运行时用于创建选项菜单的事件方法,在打开界面时,会自动调用。
@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);
//添加菜单,参数(组ID,当前选项ID,排序,标题)
//menu.add(0, 100, 1, "设置游戏");
//menu.add(0, 200, 2, "开始游戏");
//menu.add(0, 300, 3, "退出游戏");
return true;
}
//菜单的单击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int id = item.getItemId();
switch (id) {
case R.id.game_set:
Toast.makeText(this, "正在打开设置界面", Toast.LENGTH_SHORT).show();
break;
case R.id.game_start:
Toast.makeText(this, "正在启动游戏", Toast.LENGTH_SHORT).show();
break;
case R.id.game_end:
Toast.makeText(this, "正在退出游戏", Toast.LENGTH_SHORT).show();
break;
default:
break;
方法二:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
添加菜单,参数(组ID,当前选项ID,排序,标题)
menu.add(0, 100, 1, "设置游戏");
menu.add(0, 200, 2, "开始游戏");
menu.add(0, 300, 3, "退出游戏");
return true;
}
//菜单的单击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int id = item.getItemId();
switch (id) {
case 100:
Toast.makeText(this, "正在打开设置界面", Toast.LENGTH_SHORT).show();
break;
case 200:
Toast.makeText(this, "正在启动游戏", Toast.LENGTH_SHORT).show();
break;
case 300:
Toast.makeText(this, "正在退出游戏", Toast.LENGTH_SHORT).show();
break;
default:
break;
(2)Context menu 上下文菜单
相当于Windows的右击弹出的框。
一个界面只有一个上下文菜单。
(3)Popup menu 弹出菜单
private Button buttoSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttoSelect = (Button) findViewById(R.id.button_select);
}
//按钮的单击事件
public void typeSizeClick(View v){
//
PopupMenu popupMenu = new PopupMenu(this,v);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.main, popupMenu.getMenu());
//弹出式菜单选项的单击事件
popupMenu.setOnMenuItemClickListener(this);
popupMenu.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.s:
Toast.makeText(this, "你选了s号", Toast.LENGTH_SHORT).show();
break;
case R.id.m:
Toast.makeText(this, "你选了m号", Toast.LENGTH_SHORT).show();
break;
case R.id.l:
Toast.makeText(this, "你选了l号", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/s"
android:orderInCategory="100"
android:title="S"/>
<item
android:id="@+id/m"
android:orderInCategory="200"
android:title="M"/>
<item
android:id="@+id/l"
android:orderInCategory="300"
android:title="L"/>
</menu>
<Button
android:id="@+id/button_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择型号"
android:onClick="typeSizeClick" />
6:ViewPager
(1) 介绍:v4包,包含了导航,页面菜单的使用,通过适配器PagerAdapter来使用
(2)
第二天
1:PopupWindow
(1)偏移的,
(2) 无偏移的
获取屏幕的代码:
//获取屏幕的尺寸
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
2:Notifications通知
(1)普通视图
(2)宽式图
3:自定义通知
RemoteViews支持FrameLayout,LinearLayout,RelativeLayout,
3.0以下,不支持
4:样式(Style) 主题(themes)
(1)在values,中styles中
<style name = "textView_style">
<item name = "android:layout_width">wrap_content</item>
<item name = "android:layout_height">wrap_content</item>
<item name = "android:textColor">@android:color/holo_blue_dark</item>
<item name = "android:textSize">35dp</item>
<item name = " android:background">@android:color/secondary_text_light_nodisable</item>
</style>
(2)在xml中引用
<TextView
style="@style/textView_style"
android:text="@string/hello_world"
/>
(3)想更改的话,
例如:颜色
android:textColor="@android:color/holo_blue_dark"
就可以改成其他的。
(4)也继承Appthemes
5:Themes,应用在整个Activity的样式
在主清单中应用,Appliction中,Activity中,在xml文件中
6:自定义组件?
构建复杂的组件
方法一:
组合现在有的组件:继承ViewGroup,或者其子Layout类等布局进行组合
方法二:
继承View的子类或具体类,继承Button,进行修改
方法三:
完全自定义,继承View
第一步:先自定义:attrs.xml
<resources>
<declare-styleable name = "MyView">
<attr name = "textColor" format = "color"></attr>
<attr name = "textSize" format = "dimension"></attr>
<attr name = "textText" format = "String"></attr>
</declare-styleable>
</resources>
第二步:新建一个类:
例如:
public class MyView extends View{
private int textColor;
private int textSize;
private String text;
private Paint paint;//画笔
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();//实例化
//获取配置文件中的属性值
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
//textColor = array.getColor(R.styleable, MyView_textColor,0XFFFFFF);
textSize = array.getDimension(R.styleable.MyView_textSize, 24);
text = array.getString(R.styleable.MyView_text);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
//视图的绘制事件方法
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setColor(textColor);
paint.setTextSize(textSize);
//画布
canvas.drawText(text, 10, 10, paint);
}
}
第三步:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义组件" />
<com.example.android_view_zdingyi.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:textColor = "#ff0000"
app:textSize = "35sp"
app:text = ""/>
</com.example.android_view_zdingyi.MyView>
第四步:
运行