Android-《第一行代码》阅读收获
2015-12-08 15:34
453 查看
1.如何隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
2.自定义Toast
最基础的Toast
可以设置Toast的显示位置差数的意义 a Gravity constant, an x-position offset, and a y-position offset.
那么到该怎么
然后通过setView方法
就是这么简单
3.Menu
分为3类
Options menu and action bar 选项菜单
[b]Context menu and contextual action mode 上下文菜单 当长时间点击某些东西的时候响应
[/b]
[b][b]Popup menu 弹出菜单
[/b][/b]
首先是编写main.xml menu标签 如:
其中 showAsAction的解析是
action bar.
下面是这些属性的具体语法:
下面就来看看如何使用吧
重写方法
inflater.inflate(R.menu.game_menu, menu);
return true;
}[/code]
在onOptionsItemSelected里面响应事件
你可以在Menu里面声明onClick方法
然后在Activity里面写这个方法 这个方法里面接受一个参数
另外你也可以重写onPrepareOptionsMenu()这个方法 在运行的时候复写这个方法
同样有两种的方式:floating context menu contextual
action mode. 第二种是3.0以后才有的
详细情况请看Android的官网文档
requestWindowFeature(Window.FEATURE_NO_TITLE);
2.自定义Toast
最基础的Toast
Context context = getApplicationContext(); CharSequence text = "Hello toast!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show();
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
可以设置Toast的显示位置差数的意义 a Gravity constant, an x-position offset, and a y-position offset.
那么到该怎么
Creating a Custom Toast View
首先写一个xml( toast_layout.xml)<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8dp" android:background="#DAAA" > <ImageView android:src="@drawable/droid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout>
然后通过setView方法
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("This is a custom toast"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();
就是这么简单
3.Menu
分为3类
Options menu and action bar 选项菜单
[b]Context menu and contextual action mode 上下文菜单 当长时间点击某些东西的时候响应
[/b]
[b][b]Popup menu 弹出菜单
[/b][/b]
首先是编写main.xml menu标签 如:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" android:showAsAction="ifRoom"/> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /> </menu>
其中 showAsAction的解析是
android:showAsActionSpecifies when and how this item should appear as an action item in the
action bar.
下面是这些属性的具体语法:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@[+][package:]id/resource_name" android:title="string" android:titleCondensed="string" android:icon="@[package:]drawable/drawable_resource_name" android:onClick="method name" android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"] android:actionLayout="@[package:]layout/layout_resource_name" android:actionViewClass="class name" android:actionProviderClass="class name" android:alphabeticShortcut="string" android:numericShortcut="string" android:checkable=["true" | "false"] android:visible=["true" | "false"] android:enabled=["true" | "false"] android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" /> <group android:id="@[+][package:]id/resource name" android:checkableBehavior=["none" | "all" | "single"] android:visible=["true" | "false"] android:enabled=["true" | "false"] android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" > <item /> </group> <item > <menu> <item /> </menu> </item> </menu>
下面就来看看如何使用吧
Creating an Options Menu
首先你得知道2.3之前和和3.0之后的选项菜单是不一样的 2.3是在底部弹出 3.0是在actionBar那里 是不一样的重写方法
onCreateOptionsMenu
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = [code]getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}[/code]
在onOptionsItemSelected里面响应事件
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.new_game: newGame(); return true; case R.id.help: showHelp(); return true; default: return super.onOptionsItemSelected(item); } }
你可以在Menu里面声明onClick方法
然后在Activity里面写这个方法 这个方法里面接受一个参数
MenuItem
另外你也可以重写onPrepareOptionsMenu()这个方法 在运行的时候复写这个方法
Creating Contextual Menus
主要用于ListView和GridView 需要列表项点击的同样有两种的方式:floating context menu contextual
action mode. 第二种是3.0以后才有的
详细情况请看Android的官网文档
相关文章推荐
- Android_(自定义view_2)
- 安卓Android.mk 文件语法详解
- Android制作粒子爆炸特效
- git add详解
- 继续第一个andoid工程
- 使用Criteria确定android location
- Android 资源文件的命名规范问题
- Android 牛逼哄哄的快速开发框架AndroidOne,秒杀最火的Xutils
- aa
- Android Studio Error:(1, 1) 错误: 非法字符: \65279
- 【Android开发学习笔记】【高级】【随笔】插件化——初探
- Android开发艺术探索学习-View的事件分发机制(一)
- Android Menus II-Popup Menu和Menu Groups
- android mac系统下怎么直接安装android apk到手机上
- 记录我的bug问题,关于View隐藏对其他空间的影响问题
- Android实现简单的文件上传
- Android studio 默认快捷键
- 浅谈Android界面绘制避免重复渲染
- Android studio gradle 打包 那些事
- android设备获取 关于手机-》手机名称