Android基础--ActionBar的基本用法,自定义View,标题栏

来源:互联网 发布:js 跳转url 编辑:程序博客网 时间:2024/05/22 04:24

转载请标明出处: http://blog.csdn.net/android_it/article/details/51200633 本文出自:【老甩哥的CSDN博客】


1、ActionBar的简介
ActionBar位于Activity的顶部,可用来显示activity的标题、Icon、Actions和一些用于交互的View。它也可被用于应用的导航。
ActionBar 是在Android 3.0(API 11)中加入到SK中的,想在低版本中使用ActionBar有两种选择:使用http://actionbarsherlock.com 或使用Support Library v7。

2、使用ActionBar

开发API11以下的程序,首先必须在AndroidManifest.xml中指定Application或Activity的theme是Theme.Holo或其子类,否则将无法使用ActionBar。


 <application        android:name=".base.BaseApplication"        android:allowBackup="true"        android:icon="@mipmap/icon"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">    </application>    

 <style name="AppTheme" parent="AppBaseTheme"> <style name="AppBaseTheme" parent="android:Theme.Holo.Light">        <item name="android:windowFullscreen">false</item>        <item name="android:windowIsTranslucent">true</item>    </style>   </style>


在实际的开发过程中,我们经常会使用到的是自定义的ActionBar,在开发过程中,每个activity里面都有一个标题栏,左边的是返回键,中间的是标题文字,右边的是文字或者图片按钮,我们先看下自定义的ActionBar的布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView        android:id="@+id/ivLeft"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_alignParentLeft="true"        android:layout_centerVertical="true"        android:focusable="true"        android:paddingLeft="10dp"        android:paddingRight="20dp"        android:src="@drawable/arrow_left" />    <TextView        android:id="@+id/tvTitle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="@string/hello_world"        android:textColor="@color/cwhite"        android:textSize="22sp"        android:textStyle="bold" />    <LinearLayout        android:id="@+id/llRight"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_alignParentRight="true"        android:focusable="true"        android:orientation="horizontal"        android:paddingLeft="20dp"        android:paddingRight="10dp"        android:visibility="gone"        android:gravity="center_vertical"         >        <TextView            android:id="@+id/tvRight"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:textColor="@color/cwhite"            android:textSize="16sp"            android:textStyle="bold"            android:visibility="gone"            android:gravity="center_vertical"            android:layout_gravity="center_vertical"             />        <ImageView            android:id="@+id/ivRight"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_gravity="center_vertical"            android:gravity="center_vertical"            android:visibility="gone" />    </LinearLayout></RelativeLayout>


我们会写一个BaseActivity,在这个基类里面我们会这样子去写:

private void initActionBar() {if (getActionBar() == null) {return;}getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ab_bg));//ActionBar的背景图片getActionBar().setCustomView(R.layout.layout_actionbar);//ActionBar的自定义布局文件getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);OnClickListener listener = new OnClickListener() {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.ivLeft:doLeft();break;case R.id.ivRight:doRight();break;case R.id.llRight:doRight();break;}}};getActionBar().getCustomView().findViewById(R.id.ivLeft).setOnClickListener(listener);getActionBar().getCustomView().findViewById(R.id.ivRight).setOnClickListener(listener);getActionBar().getCustomView().findViewById(R.id.llRight).setOnClickListener(listener);((TextView) getActionBar().getCustomView().findViewById(R.id.tvTitle)).setText(getTitle().toString());}

@Overridepublic void setTitle(CharSequence title) {((TextView) getActionBar().getCustomView().findViewById(R.id.tvTitle)).setText(title);}protected void showLeft(boolean flag) {if (getActionBar() == null) {return;}getActionBar().getCustomView().findViewById(R.id.ivLeft).setVisibility(flag ? View.VISIBLE : View.GONE);}protected void showRight(boolean flag) {if (getActionBar() == null) {return;}getActionBar().getCustomView().findViewById(R.id.llRight).setVisibility(flag ? View.VISIBLE : View.GONE);}protected void doLeft() {finish();}protected void doRight() {}protected void setRight(int drawRes) {if (getActionBar() == null) {return;}((ImageView) getActionBar().getCustomView().findViewById(R.id.ivRight)).setImageResource(drawRes);getActionBar().getCustomView().findViewById(R.id.ivRight).setVisibility(View.VISIBLE);}protected void setRightText(String text){if(getActionBar()==null){return;}((TextView)getActionBar().getCustomView().findViewById(R.id.tvRight)).setText(text);getActionBar().getCustomView().findViewById(R.id.tvRight).setVisibility(View.VISIBLE);}

以上就是利用ActionBar自定义了一个通用标题栏:这里值得注意的是

((TextView) getActionBar().getCustomView().findViewById(R.id.tvTitle)).setText(getTitle().toString());

设置标题的值,getTitle()的值是该activity的声明中android:label的值


value:string中的值:

  <string name="title_activity_register">注册</string>



 <activity

            android:name=".ui.activity.RegisterActivity”

            android:label="@string/ttitle_activity_register"

            android:theme="@style/NoTitleBar" >

  </activity>




((TextView) getActionBar().getCustomView().findViewById(R.id.tvTitle)).setText(getTitle().toString());


其中,getTitle()取得的值就是上述 android:label="@string/ttitle_activity_register" 的值



其他的有问题可以给我留言。




0 0
原创粉丝点击