How to set background image in actionbar android [

2019-09-22 04:41发布

I'm a new Android developer. How to setting background image in actionbar android.

I can add image background within my actionbar. but cant scaling my image.

enter image description here

My style

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

</style>

MainActivity

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setCustomView(mCustomView);

}

custom_actionbar layout

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/image" />
</RelativeLayout>

Solutions

I use this Lib >> Android-ObservableScrollView

1条回答
不美不萌又怎样
2楼-- · 2019-09-22 05:18

Please read this article: http://cyrilmottier.com/2013/05/24/pushing-the-actionbar-to-the-next-level/

There you will find example and tutorial about your questuion

And also you can set action bar background like this.

private void setActionBar() {
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setCustomView(R.layout.header_actionbar);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.actionbar_blue)));
        TextView tvHeader = (TextView) findViewById(R.id.tv_title_header_actionbar);
        TextView tvSubheader = (TextView) findViewById(R.id.tv_subtitle_header_actionbar);
        tvSubheader.setVisibility(View.GONE);
    }

call setActionBar() in your oncreate().

查看更多
登录 后发表回答