SherlockActionBar:如何针对 actionBar 调整 CustomView [英] SherlockActionBar: How to adjust CustomView against actionBar

查看:41
本文介绍了SherlockActionBar:如何针对 actionBar 调整 CustomView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我怎样才能取回标题?

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

操作栏设置:

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

到您的 RelativeLayout.因为在 Android 中默认每个视图都是从左到右添加的(如果您没有在代码中指定其他内容).

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

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

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.

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

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 >

在你的 MainActivity.class 中使用这个:

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

这篇关于SherlockActionBar:如何针对 actionBar 调整 CustomView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆