Android 使用appcompat自定义操作栏布局

Android 使用appcompat自定义操作栏布局,android,eclipse,android-actionbar,android-actionbar-compat,Android,Eclipse,Android Actionbar,Android Actionbar Compat,我有一个应用程序,带有一个包含3个imageView的操作栏,我正在为此使用appcompat库,下面是代码: private void setupActionBar() { // Inflate a "Done/Cancel" custom action bar view. final LayoutInflater inflater = (LayoutInflater) this .getSupportActionBar().getThemedCon

我有一个应用程序,带有一个包含3个imageView的操作栏,我正在为此使用appcompat库,下面是代码:

private void setupActionBar() {

     // Inflate a "Done/Cancel" custom action bar view.
      final LayoutInflater inflater = (LayoutInflater) this
          .getSupportActionBar().getThemedContext()
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      final View customActionBarView = inflater.inflate(
          R.layout.my_custom_actionbar, null);
      customActionBarView.findViewById(R.id.title);



    final ActionBar actionBar = this.getSupportActionBar();
      actionBar.setDisplayOptions(
          ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM | 
          ActionBar.DISPLAY_SHOW_HOME | 
          ActionBar.DISPLAY_SHOW_TITLE);

      actionBar.setCustomView(customActionBarView, 
              new ActionBar.LayoutParams(
                  ViewGroup.LayoutParams.MATCH_PARENT, 
                  ViewGroup.LayoutParams.MATCH_PARENT
              )
             );
}
XML:

<?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"
android:background="@color/gray" >


<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="fill_horizontal" >


    <ImageView
        android:id="@+id/facebook"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:src="@drawable/ic_fb" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center"
        android:src="@drawable/ic_hotels" />

     <ImageView    
         android:id="@+id/tweetter"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:src="@drawable/ic_tweet" 
      />
      </FrameLayout>
      </RelativeLayout>

自定义操作栏加载没有问题,但我无法获得正确的布局


我试图对XML文件做一些更改,但没有成功。那么,我忘记了什么呢?

当我试着按照您发布的方式编写代码时,我得到了以下结果:

 ________________________________
|                                |  
| facebook     tweeter      logo | 
|________________________________|  
那么,我猜
layou\u gravity
属性是错误的。而且,你似乎左右颠倒。而
FrameLayout
在您的情况下似乎毫无用处(-我等待您的回答,等待我的评论)。此布局可能会达到您想要的效果:

<?xml version="1.0" encoding="utf-8"?>
<!-- Use fill_horizontal to get the whole actionbar
And center_vertical gravity on all the child views -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray"
    android:layout_gravity="fill_horizontal"
    android:gravity="center_vertical" >

    <!-- Use layout_alignParentRight attribute -->
    <ImageView
        android:id="@+id/facebook"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_fb"
        android:layout_alignParentRight="true" />

    <!-- Use layout_alignParentLeft attribute -->
    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_hotels"
        android:layout_alignParentLeft="true" />

    <!-- Use layout_toLeftOf (id) attribute -->
    <ImageView    
        android:id="@+id/tweetter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_tweet"
        android:layout_toLeftOf="@id/facebook" />

</RelativeLayout>  

什么是
logo
imageview?它是否替换左侧的应用程序图标?另外,FrameLayout的目的是什么?它起作用了,非常感谢您的解释,我才刚刚开始android开发。。只有一件事,我已经改变了我的标志的高度和宽度,因为我使用了一个大的图像作为图标的尺寸,你可以从文档中看到,特别是。此外,您还需要将px转换为dp,然后进行反向转换。这可能会对你有帮助,乔科。
 ________________________________
|                                |  
| logo         tweeter  facebook | 
|________________________________|