SherlockActionBar: 如何调整CustomView与actionBar对齐

14

问题:

如何恢复标题?


这是缺失标题的屏幕截图:

enter image description here

Actionbar设置:

 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 >


2
你能发布mLogoView吗? - Blackbelt
@blackbelt 你要的来了 :) - Roy Lee
1
ImageView的父布局宽高属性为match_parent。您可以尝试将RelativeLayout的宽高属性设置为wrap_content。 - Blackbelt
1
你有为 ImageView 设置 layout_gravity 属性吗?android:layout_gravity="right" - Blackbelt
1
@blackbelt 我们有解决方案了,快来看看答案吧,我的朋友 :) - Roy Lee
显示剩余4条评论
1个回答

30

我能想到的第一件解决方法是尝试进行设置

<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中,默认情况下每个视图都是从左到右添加的(除非在代码中指定了其他内容)。

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

编辑:

我发现在ActionBar中使用RelativeLayout和自定义视图存在一些问题,因此更好的选择是使用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);

感谢出现!:)第一个选项不起作用。宽度似乎接管了标题空间。这在导航栏中不会发生:http://stackoverflow.com/questions/16274077/how-to-increase-icon-size-of-actionbutton-in-sherlockactionbar - Roy Lee
当将宽度设置为RelativeLayout时,您能看到发生了什么吗? - hardartcore
1
你无法对齐ImageView或布局吗? - hardartcore
1
好的,你去做吧,我会检查的。 - hardartcore
2
因为在xml中,您无法将布局参数设置为根布局 - LinearLayout,但是在代码中,您可以在向ActionBar添加自定义视图时这样做。这就是整个想法。 - hardartcore
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接