SherlockActionBar:如何调整CustomView对动作条动作、SherlockActionBar、CustomView

2023-09-12 21:46:55 作者:嗜血冰封

问:

我怎么能有标题了?

下面是截图(缺少标题):

动作条设置:

 动作条动作条= getSupportActionBar();
 actionBar.setTitle(我的标题);
 actionBar.setIcon(drawable.dropdown_user);

 查看mLogoView = LayoutInflater.from(本).inflate(R.layout.mLogoView,NULL);
 actionBar.setCustomView(mLogoView);

 actionBar.setDisplayShowCustomEnabled(真正的);
 actionBar.setDisplayShowTitleEnabled(真正的);
 actionBar.setDisplayHomeAsUpEnabled(真正的);
 

mLogoView.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =match_parent>
    < ImageView的
        机器人:ID =@ + ID / img_merchant
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:SRC =@可绘制/ infoflex>
< / ImageView的>
< / RelativeLayout的>
 

解决方案

这是我能想到的方式来解决这个问题的第一件事是尝试设置

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =match_parent
机器人:layout_gravity =权与GT;
 
马云周一退休 疯传 阿里最新回应 退休 说法错误

你的 RelativeLayout的。因为作为默认的Andr​​oid中每一个观点是由左到右(如果你不指定在code别的东西)补充说。

如果不工作,我想补充机器人:layout_width =90dip或依赖的形象和你的测试一些其他的价值。第二个选项是更像是一种解决方法,但我认为它会在所有的设备上。

编辑:

当我发现有使用一点小问题 RelativeLayout的和动作条自定义视图,从而更好的选择是使用的LinearLayout ,并通过code设置的LayoutParams 。修改XML看起来是这样的:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>
    < ImageView的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:SRC =@可绘制/ ASUS/>
< / LinearLayout中>
 

和你MainActivity.class使用这样的:

 进口com.actionbarsherlock.app.ActionBar.LayoutParams;
....
动作条动作条= getSupportActionBar();

actionBar.setTitle(OMG);

的LayoutParams LP =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.RIGHT | Gravity.CENTER_VERTICAL);
查看customNav = LayoutInflater.from(本).inflate(R.layout.img,NULL); //布局,其中包含您的按钮。
actionBar.setCustomView(customNav,LP);
actionBar.setDisplayShowCustomEnabled(真正的);
 

Question:

How can I have the title back?

Here's the screenshot (Missing title):

Actionbar setting:

 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 >

解决方案

The first thing which I can think a way to fix that is try to set

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

to your RelativeLayout. Because as default in Android every View is added from left to right (if you doesn't specify something else in your code).

And if that doesn't work, I would add android:layout_width="90dip" or some other value depending on the image and your tests. The second option is more like a workaround, but I think it will work on all devices.

Edit:

As I found there is a little problem using RelativeLayout and Custom Views in ActionBar, so the better option is to use LinearLayout and set LayoutParams via code. Change your xml to look like this :

<?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 >

and in your MainActivity.class use this :

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);