android - 使用 appcompat 自定义操作栏布局

标签 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().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>

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

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

最佳答案

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

 ________________________________
|                                |  
| facebook     tweeter      logo | 
|________________________________|  

然后,我猜 layout_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         tweeter  facebook | 
|________________________________|  

关于android - 使用 appcompat 自定义操作栏布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330513/

相关文章:

android - RXJava + 改造。每 n 秒重复一次请求

java - import javax.xml.transform 无法解析

android - viewModelScope.launch(Dispatchers.IO) 用途

android - 工具栏的 Logo 图标是否可点击?

android - 更改 fragment 中的 ActionBar 颜色

android - 如何修复 "SDK location not found..."?

eclipse - 配置位置无效

java - Android 版 Eclipse 上的 Admob 库导入失败

java - Android 测试类在 Eclipse 中编译失败,出现 "bound mismatch"错误

android - 如何以编程方式减少操作栏选项卡的宽度