动作条图案填充左 [英] Actionbar logo padding left

查看:121
本文介绍了动作条图案填充左的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的动作条显示上的标识的左边的空间。此空间为upindicator我想保留。

我想给这个空间定义20dp例如距离。

我怎样才能做到这一点?

我可以设置图标的填充为0,但不会去除标志和在屏幕的左侧之间的空间。

我试图用一个自定义布局来解决它。但是有一个小问题。每次活动的推出,首先是正常的动作条​​出现,然后显示自定义布局。

这是我怎么显示我的自定义视图:

  @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_custom);
        //设置操作栏。
        最后的动作条动作条= getActionBar();
        //设置自定义动作条
        actionBar.setDisplayShowCustomEnabled(真正的);
        actionBar.setCustomView(R.layout.actionbar);
        //显示定制设计的动作条
        actionBar.setDisplayUseLogoEnabled(假);
        actionBar.setDisplayHomeAsUpEnabled(假);
        actionBar.setDisplayShowHomeEnabled(假);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    }
 

这是我在styles.xml:

 <! - 自定义动作条 - >
        <项目名称=机器人:windowActionBarOverlay>假< /项目>
        <项目名称=机器人:actionBarStyle> @风格/ MyActionBar< /项目>
        <项目名称=机器人:actionButtonStyle> @风格/ MyActionButton< /项目>

    < /风格>

    <样式名称=MyActionBar父=@安卓风格/ Widget.Holo.Light.ActionBar>
        <项目名称=机器人:背景> @色/ Green2的< /项目>
        <项目名称=机器人:图标> @可绘制/ app_icon_small< /项目>
        <! - 支持库的兼容性 - >

    < /风格>

    <样式名称=MyActionButton父=@安卓风格/ Widget.Holo.Light.ActionButton>
        <项目名称=机器人:paddingRight> @扪/ padding_border< /项目>
        <项目名称=机器人:以下属性来> @扪/ padding_border< /项目>
        <项目名称=机器人:SRC> @可绘制/ icon_plus< /项目>
        !<  - <项目名称=安卓了minWidth> 0dp< /项目>  - >
        !<  - <项目名称=机器人:以下属性来> 0dp< /项目>  - >
        !<  - <项目名称=机器人:paddingRight> 15dp< /项目>  - >
        <! - 支持库的兼容性 - >
    < /风格>
 

这是自定义视图的布局:

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =match_parent
    机器人:重力=center_vertical
    机器人:背景=@色/ Green2的>
    < ImageView的
        机器人:ID =@ + ID /头
        机器人:contentDescription =@字符串/ APP_NAME
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:以下属性来=@扪/ padding_border
        机器人:paddingRight =@扪/ padding_border
        机器人:SRC =@可绘制/ app_icon_small/>

    <的EditText
        机器人:ID =@ + ID / et_actionbar_title
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=YOLO
        机器人:重力=中心
        机器人:文字颜色=@机器人:彩色/黑白
        机器人:TEXTSTYLE =黑体
        机器人:背景=@机器人:彩色/透明
        />
< / LinearLayout中>
 

解决方案

您可以创建文件夹/ RES /绘制xml文件(your_xml_file),像这样的:

 < XML版本=1.0编码=UTF-8&GT?;
<层列表
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <项目
        机器人:可绘制=@可绘制/ menu_button
        机器人:右=20dp/>


< /层列表>
 

和它添加到code:

 <活动机器人:名称= ...
            机器人:标志=@可绘制/ your_xml_file>
 

The actionbar shows a space on the left side of the logo. This space is reserved for the upindicator i think.

I want to give this space a defined distance of 20dp for example.

How can i do this?

I can set the padding of the icon to 0, but that does not remove the space between the logo and the left side of the screen.

I tried to solve it by using a custom layout. But there is a little problem. Every time the activity launches, first the normal actionbar appears and then shows the custom layout.

This is how i show my custom view:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom);
        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        //set custom actionbar
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(R.layout.actionbar);
        //Displays the custom design in the actionbar
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    }

This is in my styles.xml:

        <!--Customize the actionbar-->
        <item name="android:windowActionBarOverlay">false</item>
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionButtonStyle">@style/MyActionButton</item>

    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/green2</item>
        <item name="android:icon">@drawable/app_icon_small</item>
        <!-- Support library compatibility -->

    </style>

    <style name="MyActionButton" parent="@android:style/Widget.Holo.Light.ActionButton">
        <item name="android:paddingRight">@dimen/padding_border</item>
        <item name="android:paddingLeft">@dimen/padding_border</item>
        <item name="android:src">@drawable/icon_plus</item>
        <!--<item name="android:minWidth">0dp</item>-->
        <!--<item name="android:paddingLeft">0dp</item>-->
        <!--<item name="android:paddingRight">15dp</item>-->
        <!-- Support library compatibility -->
    </style>

This is the layout of the custom view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:background="@color/green2">
    <ImageView
        android:id="@+id/header"
        android:contentDescription="@string/app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/padding_border"
        android:paddingRight="@dimen/padding_border"
        android:src="@drawable/app_icon_small"/>

    <EditText
        android:id="@+id/et_actionbar_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="YOLO"
        android:gravity ="center"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:background="@android:color/transparent"
        />
</LinearLayout>

解决方案

You can create xml file(your_xml_file) in folder /res/drawable, like this:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:drawable="@drawable/menu_button"
        android:right="20dp"/>


</layer-list>

and add it to code:

<activity android:name=...
            android:logo="@drawable/your_xml_file" >

这篇关于动作条图案填充左的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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