Reputation: 9899
To put a right-aligned spinner on action bar, I have to use a custom view with relative layout. But after I put custom view on the action bar, I find that app title disappear, it must be impacted by putting custom view on action bar.
Is it possible to display both title and custom view on action bar? Otherwise I have to put title in customer view as well, but I don't want do go that way.
Upvotes: 5
Views: 1745
Reputation: 883
If your 'action bar' is just a simple view, my solution is put some code to BaseActivity, it seems like:
@Override
public void setContentView(@LayoutRes int layoutResID) {
super.setContentView(layoutResID);
titleBar = (TitleBar) findViewById(R.id.title_view);
if (titleBar != null) {
titleBar.setTitle(getTitle());
}
}
When you set the label of activity and call setContentView
method, the custom view will display the label as title.
PS:
My TitleBar.java
code:
public class TitleBar extends FrameLayout {
TextView back, title, action;
public TitleBar(Context context) {
super(context);
init(context);
}
public TitleBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public TitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
setId(R.id.title_view);
View layout = View.inflate(context, R.layout.title_bar, null);
back = (TextView) layout.findViewById(R.id.back);
title = (TextView) layout.findViewById(R.id.title);
action = (TextView) layout.findViewById(R.id.action);
addView(layout);
ViewGroup.LayoutParams params = layout.getLayoutParams();
setLayoutParams(params);
}
public void setTitle(CharSequence content) {
title.setText(content);
}
public void setTitleClickListener(View.OnClickListener listener) {
title.setOnClickListener(listener);
}
public void setBackClickListener(View.OnClickListener listener) {
back.setOnClickListener(listener);
}
public void setActionClickListener(View.OnClickListener listener) {
action.setOnClickListener(listener);
}
public void setActionVisiable(boolean visiable) {
action.setVisibility(visiable ? VISIBLE : INVISIBLE);
}
public void setBackVisiable(boolean visiable) {
back.setVisibility(visiable ? VISIBLE : INVISIBLE);
}
}
Upvotes: 0
Reputation: 1593
Actually it doesn't "replace". This can be shown by the hierarchy viewer. it's just that if you set your custom view too big, it will be ON TOP OF the title. however, if you only put something like a spinner, you can set it to be right aligned and the title will be shown.
Here's XML I used to put an image view on the right:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="68dp"
android:layout_height="31dp"
android:padding="4dp"
android:layout_gravity="center_vertical|right"
android:layout_marginRight="4dp"
android:onClick="onImageClicked"
android:background="@drawable/mypic_bg"
android:src="@drawable/logo_mypic" />
and in code:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_HOME_AS_UP
| ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(R.layout.my_title_bar);
Upvotes: 2
Reputation: 2086
The custom view will replace the Title bar. I suggest you to include a textview for the app title in your custom view so that you have more ground to play.
Upvotes: 4