Android : Styling the Action Bar

on Tuesday, March 31, 2015


I'm trying to style the action bar by adding a custom back button on the left and an icon in the center. The rendering in Android Studio is exactly as I want it, but when I test the layout on my phone, it just shows both back button and icon on the left, with the icon on top of the back button. This is my layout file:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/action_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageButton android:id="@+id/back_button"
android:contentDescription="Image"
android:src="@drawable/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@color/action_bar_bg_color"
android:layout_alignParentLeft="true"/>

<ImageView android:id="@+id/cow_icon"
android:contentDescription="Image"
android:src="@drawable/cowhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"/>

</RelativeLayout>


And this is what I do to style the action bar:



ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
View mActionBar = getLayoutInflater().inflate(R.layout.action_bar_layout, null);
actionBar.setCustomView(mActionBar);
actionBar.setDisplayShowCustomEnabled(true);


Anyone see what could be causing the flawed styling of the action bar?


0 comments:

Post a Comment