(Android的一室公寓)自定义操作栏布局心不是填充整个行动起来吧 [英] (Android Studio) Custom Action Bar Layout isnt filling whole action bar

查看:297
本文介绍了(Android的一室公寓)自定义操作栏布局心不是填充整个行动起来吧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前的setContentView()

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));

在ab.xml

in ab.xml

在我的应用程序

INT styles.xml    

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

推荐答案

答案 改变你的风格像这样

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>

和你ActionBar.xml布局是这样的: 注意:如果你改变了'机器人:layout_height =ATTR / actionBarSize?到机器人:layout_height =match_parent它不会填补整个操作栏的宽度,如果你改变的LinearLayout为RelativeLayout的与机器人:layout_height =match_parent操作栏将填满整个屏幕

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>

最后:

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.

这篇关于(Android的一室公寓)自定义操作栏布局心不是填充整个行动起来吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆