android - SherlockActionBar : How to adjust CustomView against actionBar

标签 android android-actionbar actionbarsherlock android-custom-view

问题:

我怎样才能取回标题?


这是屏幕截图(缺少标题):

enter image description here

操作栏设置:

 ActionBar actionBar = getSupportActionBar();
 actionBar.setTitle("My Title");
 actionBar.setIcon(drawable.dropdown_user);

 View mLogoView = LayoutInflater.from(this).inflate(R.layout.mLogoView, null);
 actionBar.setCustomView(mLogoView);

 actionBar.setDisplayShowCustomEnabled(true);
 actionBar.setDisplayShowTitleEnabled(true);
 actionBar.setDisplayHomeAsUpEnabled(true);

mLogoView.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >
    <ImageView
        android:id="@+id/img_merchant"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true"
        android:src="@drawable/infoflex">
</ImageView>
</RelativeLayout >

最佳答案

我能想到的第一件事就是尝试设置

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right" >

到您的 RelativeLayout。因为在 Android 中默认情况下,每个 View 都是从左到右添加的(如果您没有在代码中指定其他内容)。

如果这不起作用,我会根据图像和您的测试添加 android:layout_width="90dip" 或其他一些值。第二种选择更像是一种解决方法,但我认为它适用于所有设备。

编辑:

因为我发现在 ActionBar 中使用 RelativeLayout 和自定义 View 有一点问题,所以更好的选择是使用 LinearLayout 并设置 LayoutParams 通过代码。将您的 xml 更改为如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/asus"    />
</LinearLayout >

并在您的 MainActivity.class 中使用:

import com.actionbarsherlock.app.ActionBar.LayoutParams;
....
ActionBar actionBar = getSupportActionBar();

actionBar.setTitle("OMG");

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(R.layout.img, null); // layout which contains your button.
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);

关于android - SherlockActionBar : How to adjust CustomView against actionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16516306/

相关文章:

Android 将数据库从内部存储复制到外部存储

android - Google Plus 磁贴动画

android - 通过xml更改Android ActionBar中的标题文本颜色

android - Android 操作栏中的三个点设置菜单未出现

Android 2.2 Spinner 在 Actionbar 中看起来很旧

Android Sherlock VS AppCompat

java - 如何生成具有嵌套依赖项的 AAR

android - 更改操作栏中菜单项的 View

android - 分隔符不适用于带有 viewpagerindicator 和 actionbarsherlock 的 TitlePageIndicator

android - TextInputEditText 的数据绑定(bind)和选择