Android Api Demos登顶之路(三十)Display Options
来源:互联网 发布:mac表情 编辑:程序博客网 时间:2024/06/02 01:23
这个demo演示了actionbar的各种布局样式。
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp" > <Button android:id="@+id/display_home_as_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DISPLAY_HOME_AS_UP" /> <Button android:id="@+id/display_show_home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DISPLAY_SHOW_HOME" /> <Button android:id="@+id/display_use_loge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DISPLAY_USE_LOGO" /> <Button android:id="@+id/display_show_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DISPLAY_SHOW_TITLE" /> <Button android:id="@+id/display_show_custom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DISPLAY_SHOW_CUSTOM" /> <Button android:id="@+id/navigation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NAVIGATION" /> <Button android:id="@+id/cycle_custon_view_gravity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cycle Custon View Gravity" /> <Button android:id="@+id/toggle_visibility" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Toggle Visibility" /> <Button android:id="@+id/toggle_system_ui" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TOGGLE SYSTEM UI" /> </LinearLayout></ScrollView>
menu_item.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_item" android:title="Menu Title" android:showAsAction="never"/></menu>
custom_view.xml
<?xml version="1.0" encoding="utf-8"?><Button xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custon View" ></Button>
配置文件中添加logo属性
<activity android:name=".MainActivity" android:label="@string/app_name" android:logo="@drawable/apidemo_androidlogo"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
MainActivity
@SuppressWarnings("deprecation")public class MainActivity extends Activity implements OnClickListener, TabListener { private View mCustomView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.display_home_as_up).setOnClickListener(this); findViewById(R.id.display_show_home).setOnClickListener(this); findViewById(R.id.display_show_custom).setOnClickListener(this); findViewById(R.id.display_show_title).setOnClickListener(this); findViewById(R.id.display_use_loge).setOnClickListener(this); findViewById(R.id.navigation).setOnClickListener(this); findViewById(R.id.cycle_custon_view_gravity).setOnClickListener(this); findViewById(R.id.toggle_visibility).setOnClickListener(this); findViewById(R.id.toggle_system_ui).setOnClickListener(this); mCustomView = getLayoutInflater().inflate(R.layout.custom_view, null); ActionBar bar = getActionBar(); // 向actionbar中添加自定义视图 bar.setCustomView(mCustomView); // 向actionbar中添加导航标签 bar.addTab(bar.newTab().setText("TAB 1").setTabListener(this)); bar.addTab(bar.newTab().setText("TAB 2").setTabListener(this)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_item, menu); return true; } @Override public void onClick(View v) { int flag = 0; ActionBar bar = getActionBar(); switch (v.getId()) { case R.id.display_home_as_up: //显示向上的箭头 flag = ActionBar.DISPLAY_HOME_AS_UP; break; case R.id.display_show_home: //显示icon flag = ActionBar.DISPLAY_SHOW_HOME; break; case R.id.display_show_custom: //显示自定义View flag = ActionBar.DISPLAY_SHOW_CUSTOM; break; case R.id.display_show_title: flag = ActionBar.DISPLAY_SHOW_TITLE; break; case R.id.display_use_loge: flag = ActionBar.DISPLAY_USE_LOGO; break; case R.id.navigation: bar.setNavigationMode(bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD ? ActionBar.NAVIGATION_MODE_TABS : ActionBar.NAVIGATION_MODE_STANDARD); break; case R.id.cycle_custon_view_gravity: ActionBar.LayoutParams lp = (LayoutParams) mCustomView .getLayoutParams(); int newGravity = 0; /** * 1.如果两个数都是奇数进行按位与(&)运算结果就是较小的奇数 * 2.如果两个数都是奇数进行按位或(|)运算结果就是较大的奇数 * 3.对奇数进行按位取反(~)运算得到的是偶数 * 4.奇数与偶数进行&运算结果恒为0 * 我们可以观察这里的Gravity的几个常量都是奇数而且Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK * 值最大 */ switch (lp.gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) { case Gravity.START: newGravity = Gravity.CENTER_HORIZONTAL; break; case Gravity.CENTER_HORIZONTAL: newGravity = Gravity.END; break; case Gravity.END: newGravity = Gravity.START; break; } /* * lp.gravity & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK的结果恒为0 * 0| newGravity结果恒为newGravity。所以这里进行了复杂的运算其实就是相当于 * lp.gravity=newGravity; */ /*lp.gravity = lp.gravity & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK | newGravity;*/ lp.gravity=newGravity; bar.setCustomView(mCustomView, lp); break; case R.id.toggle_visibility: if (bar.isShowing()) { bar.hide(); } else { bar.show(); } break; case R.id.toggle_system_ui: if ((getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN) != 0) { getWindow().getDecorView().setSystemUiVisibility(0); }else{ getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN); } break; } /* * 1.当在配置文件中为activity设置了android:logo属性时, logo会代替icon出现在actionbar的左侧。 * 2.如果要单独显示logo必须DISPLAY_USE_LOGO与DISPLAY_SHOW_HOME同时设置为true。 * 3.默认情况下actionbar的显示布局为DISPLAY_SHOW_TITLE(值为8)、DISPLAY_SHOW_HOME(值为2) * DISPLAY_USE_LOGO(值为1),相当于bar.setDisplayOptions( * DISPLAY_SHOW_TITLE|DISPLAY_SHOW_HOME|DISPLAY_USE_LOGO)。 * 我们知道^(异或)的特点一是相当于半加运算(不带进位的加法运算)二是a^b=c,c^b=a(即经过两次 * 异或运算又回到原来的值)。 * 所以在默认情况下我们使用bar.getDisplayOptions()得到的值是11即由8^2^1得到,也就是我们 * 将TITLE、HOME、LOGO三个常量置为true的结果。 * 4.setDisplayOptions(change, flag);方法只有当change与flag中同时包含某一常量时 * 该常量才会在actionbar中显示。 * 5.默认状态下bar.getDisplayOptions()的值为11包含三个常量,如果flag为DISPLAY_HOME_AS_UP * 则change的值为15,则此时chang当中就包含了四个常量 * DISPLAY_SHOW_TITLE|DISPLAY_SHOW_HOME|DISPLAY_USE_LOGO|DISPLAY_HOME_AS_UP * 当执行bar.setDisplayOptions(change, flag);时因为change和flag中同时包含了DISPLAY_HOME_AS_UP * 所以DISPLAY_HOME_AS_UP会显示,而其余依旧保持默认状态。 * 当再次点击按钮再次执行change = bar.getDisplayOptions() ^ flag;由于是第二次异或 * 相当于将DISPLAY_HOME_AS_UP从change中去除了,所以此时长change中就只有三个常量了 * DISPLAY_SHOW_TITLE|DISPLAY_SHOW_HOME|DISPLAY_USE_LOGO,再执行 * bar.setDisplayOptions(change, flag)由于只有flag中有DISPLAY_HOME_AS_UP * 就会将DISPLAY_HOME_AS_UP隐藏了。其余按钮运算结果也是一样的。 */ int change = bar.getDisplayOptions() ^ flag; /* * System.out.println("options:"+bar.getDisplayOptions()); * System.out.println("flag:"+flag); * System.out.println("change:"+change); */ // System.out.println("~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK:"+(Gravity.END // & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK )); // System.out.println("~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK:"+(Gravity.START // & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK )); // System.out.println("~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK:"+(Gravity.CENTER_HORIZONTAL // & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK )); // System.out.println("DISPLAY_SHOW_TITLE:"+ActionBar.DISPLAY_SHOW_TITLE); bar.setDisplayOptions(change, flag); } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub }}
0 0
- Android Api Demos登顶之路(三十)Display Options
- Android Api Demos登顶之路(五十七)Service Remote Service Binding options
- Android Api Demos登顶之路(二)
- Android Api Demos登顶之路(三)
- Android Api Demos登顶之路(四)
- Android Api Demos登顶之路(五)
- Android Api Demos登顶之路(六)
- Android Api Demos登顶之路(七)
- Android Api Demos登顶之路(八)
- Android Api Demos登顶之路(九)
- Android Api Demos登顶之路(十)Overscan
- Android Api Demos登顶之路(十一)Persistent state
- Android Api Demos登顶之路(十二)Presentation
- Android Api Demos登顶之路(十四)Quick Contacts
- Android Api Demos登顶之路(十五)Receive Result
- Android Api Demos登顶之路(十六)Recreate
- Android Api Demos登顶之路(十七)Redirection
- Android Api Demos登顶之路(十八)Recorder Activities
- c#对mysql的批量插入
- NZND壳-设计
- 黑马程序员-----Java基础-----继承
- 用CocoaPods做iOS程序的依赖管理
- 剑指Offer面试题51(Java版):数组中重复的数字
- Android Api Demos登顶之路(三十)Display Options
- 移动时代的前端加密
- cassandra nodejs driver maillist link
- Scala入门到精通——第二十四节 高级类型 (三)
- jdists 一款强大的代码块预处理工具
- POJ 2559 单调栈模板题
- OpenCV_局部图像特征的提取与匹配_源代码
- 【小熊刷题】Longest Substring Without Repeating Characters
- 连载六-------视图控制器