SherlockActionBar: how to configure CustomView vs actionBar

Question:

How to return the title?




Here is a screenshot (Missing title):

enter image description here

Customize the action bar:

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 > 



+14
android actionbarsherlock android-actionbar android-custom-view
May 13, '13 at 6:51
source share
1 answer

The first thing I can fix is ​​to try to install

 <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 . Since by default in Android each view is added from left to right (unless you 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 small problem using RelativeLayout and custom views in ActionBar, so it is better to use LinearLayout and set LayoutParams through code. Modify your xml so it looks 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); 
+29
May 13 '13 at 8:06
source share



All Articles