如何Center图标在Android的操作栏 [英] How To Center Icon in Android Action Bar

查看:192
本文介绍了如何Center图标在Android的操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想集中在Android操作栏中的图标。我使用的自定义布局具有在左侧的按钮时,在中心的图标和在右侧的菜单选项。

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / ActionBarWrapper
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>



        <的ImageButton
            机器人:ID =@ + ID / slideMenuButton
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:SRC =@可绘制/ ic_menu_bookmark/>

        < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:ID =@ + ID / RelativeLayout1
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=垂直
            机器人:layout_centerInParent =真正的>
            < ImageView的
                机器人:ID =@ + ID /图标
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:SRC =@可绘制/ ic_launcher
                机器人:layout_centerInParent =真/>
        < / RelativeLayout的>



< / RelativeLayout的>
 

我已经试过这有和没有RelativeLayout的包裹ImageView的。左边的按钮显示了罚款,我可以设置图标与layout_toRightOf,但我不能得到它的中心。任何人有什么想法?

编辑: 下面是Android的code在上创建方法。

 动作条动作条= getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(假);
    actionBar.setDisplayUseLogoEnabled(假);
    actionBar.setDisplayHomeAsUpEnabled(假);
    actionBar.setDisplayShowCustomEnabled(真正的);
    actionBar.setDisplayShowHomeEnabled(假);
    actionBar.setDisplayOptions(actionBar.DISPLAY_SHOW_CUSTOM);
    查看CVIEW = getLayoutInflater()膨胀(R.layout.actionbar,空)。
    actionBar.setCustomView(CVIEW);
 

解决方案

好。这应该工作。试试这个(经过测试,在正常的活动):

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / ActionBarWrapper
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
     机器人:layout_gravity =中心
      机器人:方向=横向
   >

        <的ImageButton
            机器人:ID =@ + ID / slideMenuButton
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:SRC =@可绘制/ ic_launcher
             机器人:layout_alignParentLeft =真/>

            < ImageView的
                机器人:ID =@ + ID /图标
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:SRC =@可绘制/ ic_launcher
                机器人:layout_centerVertical =真
                机器人:layout_centerInParent =真/>



< / RelativeLayout的>
 

I'm trying to center the icon in the android action bar. I'm using a custom layout that has a button on the left side, an icon in the center and the menu options on the right.

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



        <ImageButton
            android:id="@+id/slideMenuButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_menu_bookmark" />

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/RelativeLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerInParent="true"  >
            <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher"
                android:layout_centerInParent="true" />
        </RelativeLayout>



</RelativeLayout>

I've tried this with and without the RelativeLayout wrapping the ImageView. The left button shows up fine, and I can set the icon with layout_toRightOf, but I can't get it to center. Anyone have any ideas?

Edit: Here's the android code in the on create method.

ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayOptions(actionBar.DISPLAY_SHOW_CUSTOM);
    View cView = getLayoutInflater().inflate(R.layout.actionbar, null);
    actionBar.setCustomView(cView);

解决方案

Okay. This should work. Try this(Tested in normal activity):

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

        <ImageButton
            android:id="@+id/slideMenuButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
             android:layout_alignParentLeft="true" />

            <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher"
                android:layout_centerVertical="true"
                android:layout_centerInParent="true"  />



</RelativeLayout>

这篇关于如何Center图标在Android的操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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