问题
before setContentView()
LayoutInflater inflater = (LayoutInflater) getSupportActionBar() .getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBarView = inflater.inflate(R.layout.ab, null);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView, new android.support.v7.app.ActionBar.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
in ab.xml
in My Application
int styles.xml
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>
<style name="ATheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">@style/AppTheme.ActionBar.a</item>
<item name="actionBarStyle">@style/AppTheme.ActionBar.a</item>>
<item name="android:icon">@android:color/transparent</item>
</style>
<style name="AppTheme.ActionBar.a" parent="Widget.AppCompat.Light.ActionBar">
<item name="android:height">50dp</item>
<item name="android:background">#d1d1d1</item>
</style>
in my Application, isn't filling action bar completely
回答1:
The answer is Change your style like this
<resources>
<!-- the theme applied to the application or activity -->
<style name="AppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="Widget.AppCompat.Toolbar">
<!-- Support library compatibility -->
<item name="contentInsetStart">0dp</item>
<!--<item name="background">@android:color/transparent</item>-->
</style>
</resources>
And your ActionBar.xml Layout like this: Note: if you changed 'android:layout_height="?attr/actionBarSize"' to 'android:layout_height="match_parent"' it will not fill whole action bar width and if you changed LinearLayout to RelativeLayout with 'android:layout_height="match_parent"' Action bar will fills the entire screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:gravity="center"
android:background="#0000FF"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="Aplication"
/>
</LinearLayout>
Finally :
setContentView(R.layout.activity_main);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.ab);
View customActionBarView = actionBar.getCustomView();
Hope this help.
来源:https://stackoverflow.com/questions/29478494/android-studio-custom-action-bar-layout-isnt-filling-whole-action-bar