自定义操作栏

时间:2013-08-24 10:17:17

标签: android android-layout

我为自定义操作栏创建了一个布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contacts_action_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#835CA1"
android:padding="1dip" >

<!-- ListRow Left side Thumbnail image -->

<LinearLayout
    android:id="@+id/thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dip"
    android:padding="3dip"
    android:layout_centerVertical="true" >

    <ImageView
        android:id="@+id/home_image"
        android:contentDescription="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cantacts_icon_action_bar" />
</LinearLayout>


<LinearLayout
    android:id="@+id/thumbnaill"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_toRightOf="@+id/thumbnail"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/contactname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dip"
        android:textColor="#FFFFFF"
        android:textSize="14sp"
        android:typeface="sans"
        android:text="Super Mario" />

    <TextView
        android:id="@+id/onlineoroffline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dip"
        android:textColor="#E876AF"
        android:textSize="12sp"
        android:typeface="sans"
        android:text="Online" />
</LinearLayout>

<LinearLayout
    android:id="@+id/thumbnail_edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dip"
    android:padding="3dip"
    android:layout_centerVertical="true" >

    <ImageView
        android:id="@+id/edit_image"
        android:contentDescription="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/edit_action_bar" />
</LinearLayout>

<LinearLayout
    android:id="@+id/thumbnail_favorites"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/thumbnail_edit"
    android:layout_marginRight="5dip"
    android:padding="3dip"
    android:layout_centerVertical="true" >

    <ImageView
        android:id="@+id/favorites_image"
        android:contentDescription="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/star_action_bar_unselected" />
</LinearLayout>


</RelativeLayout>  

给了我以下操作栏:

enter image description here

然后我在代码中设置以下方法:

ActionBar ab = getActionBar();
ab.setDisplayShowCustomEnabled(true);
ab.setCustomView(R.id.contacts_action_bar); // Id of the custom_action_bar

当我启动它时,应用程序崩溃了。甚至可以设置这样的自定义操作栏吗?

我正在尝试这样做,因为我无法以编程方式自定义它。

我知道,我可以用方法设置用红色标记的视图:

actionBar.setIcon(R.id.homeicon);
actionBar.setTitle("Some title");

我知道,我可以设置操作按钮,如here所述。

但是如何在下面的图片中添加标有蓝色的在线/离线标签?

enter image description here

编辑:Logcat输出

08-24 14:16:00.257: E/AndroidRuntime(31831):    FATAL EXCEPTION: main
08-24 14:16:00.257: E/AndroidRuntime(31831):    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appcloned/com.example.appcloned.ContactActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f0a0000 type #0x12 is not valid
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.os.Looper.loop(Looper.java:154)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.app.ActivityThread.main(ActivityThread.java:4945)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at java.lang.reflect.Method.invokeNative(Native Method)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at java.lang.reflect.Method.invoke(Method.java:511)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at dalvik.system.NativeStart.main(Native Method)
08-24 14:16:00.257: E/AndroidRuntime(31831):    Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f0a0000 type #0x12 is not valid
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2358)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.content.res.Resources.getLayout(Resources.java:952)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at com.android.internal.app.ActionBarImpl.setCustomView(ActionBarImpl.java:260)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at com.example.appcloned.ContactActivity.onCreate(ContactActivity.java:23)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.app.Activity.performCreate(Activity.java:4531)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
08-24 14:16:00.257: E/AndroidRuntime(31831):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
08-24 14:16:00.257: E/AndroidRuntime(31831):    ... 11 more

2 个答案:

答案 0 :(得分:0)

  1. 是的,可以使用自定义操作栏。事实上,大多数开发人员都使用它。
  2. 在线/离线 - 我认为这是TextView根据某个事件的触发器进行更新。
  3. 编辑:

    发布logcat!

答案 1 :(得分:0)

将xml布局用于菜单文件夹

<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/appicon"         
        android:icon="@drawable/icon"
        android:showAsAction="always"
        android:orderInCategory="1"
       >

    </item>

      <item
        android:id="@+id/iternet"
        android:title="Offline"
        android:icon="@drawable/internet"
        android:showAsAction="always"
        android:orderInCategory="2"
        >             

 </item>

<item
        android:id="@+id/edit"
        android:title="Edit"
       android:icon="@drawable/icon"
        android:showAsAction="always"
        android:orderInCategory="2"
        >             

从您的活动中调用布局

@Override
      public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.contacts_action_bar, menu);     
        return true;
      }
单击操作栏图标时的

操作       @覆盖       public boolean onOptionsItemSelected(MenuItem item){         switch(item.getItemId()){

    case R.id.iternet:
        // call  enable/disable internet function
          break;    
    default:
      break;
    }

    return true;
  }