Android的动作条setActionView布局问题 [英] Android ActionBar setActionView layout issue

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

问题描述

我一直在试图使用setActionView从动作条中的ICS

好像应该是简单的,但不知何故,我没有得到的布局调整,我会希望。正如你可以在下面的目标图标在图像中看到的是正确居中在它的布局。但是,当我setActionBar(进步)的进展情况来看总是对准无论我尝试的权利。

下面是2个状态,之前并单击该菜单项后。正如你所看到的进展观点始终靠右对齐。我试着改变了我的进步布局的xml重力选项从左至右为中心,并不管我做什么改变它似乎并没有改变任何东西。

我还没有发现关于这个问题,所以我想我必须做一些错误的任何信息。

做任何有线索? 感谢您的帮助!

下面是我的操作栏菜单布局action_bar_menu.xml

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:ID =@ + ID /查找
          机器人:标题=定位
          机器人:图标=@可绘制/查找
          机器人:showAsAction =ifRoom | withText/>
< /菜单>
 

下面是我的进度条的布局inderterminate_progress.xml

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
             机器人:layout_width =match_parent
             机器人:layout_height =match_parent
             机器人:重力=中心>

    <进度机器人:layout_width =25dp
                 机器人:layout_height =25dp
                 机器人:layout_gravity =中心
                 机器人:不确定=真
                 ?机器人:ATTR / progressBarStyleInverse的风格= />
< /的FrameLayout>
 

最后,这里是我testx活动

 公共类HelloAndroidActivity延伸活动{

    / **
     *第一次创建活动时调用。
     * @参数savedInstanceState如果该项活动之后重新初始化
     * previously被关闭,那么这个软件包中包含的数据是最
     *在最近的onSaveInstanceState(包)提供。 < B>注意:否则,它为空< / B>。
     * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        getActionBar()的setTitle(测试)。
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.action_bar_menu,菜单);
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        super.onOptionsItemSelected(项目);


        如果(R.id.locate == item.getItemId()){

            最终的菜单项菜单项= item.setActionView(R.layout.inderterminate_progress);

            新主题(新的Runnable(){
                @覆盖
                公共无效的run(){
                    SystemClock.sleep(3000);
                    runOnUiThread(新的Runnable(){
                        @覆盖
                        公共无效的run(){
                            menuItem.setActionView(空);
                        }
                    });
                }
            })。开始();
        }

        返回true;
    }
}
 

解决方案

看来,明确定义的样式和布局膨胀,如下图所示的根加入了4dip填充解决了这个问题。

 < ImageView的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:以下属性来=4dip
    机器人:paddingRight =4dip
    ?机器人:ATTR / actionButtonStyle的风格= />
 

这似乎在Android样式表<一个使用href="http://source-android.frandroid.com/frameworks/base/core/res/res/layout/action_menu_item_layout.xml">here

I've been trying to use the setActionView from the ActionBar in ICS

Seems like it should be straight forward but somehow I'm not getting the layout alignment that I would hope for. As you can see in the image below the 'target' icon is centered correctly within it's layout. But when I setActionBar(progress) the progress view is always aligned to the right whatever I try.

Here are the 2 states, before and after clicking the menu item. As you can see the progress view is always aligned to the right. I've tried changing the gravity options in the my progress layout xml from left to right to center and whatever I do change it doesn't seem to change anything.

I haven't found any info regarding this problem so I'm thinking I must be doing something wrong.

Do anyone have a clue? Thanks for the help!

Here's my action bar menu layout 'action_bar_menu.xml'

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/locate"
          android:title="locate"
          android:icon="@drawable/locate"
          android:showAsAction="ifRoom|withText" />
</menu>

Here's my progressbar layout 'inderterminate_progress.xml'

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:gravity="center">

    <ProgressBar android:layout_width="25dp"
                 android:layout_height="25dp"
                 android:layout_gravity="center"
                 android:indeterminate="true"
                 style="?android:attr/progressBarStyleInverse"/>
</FrameLayout>

And finally here's my testx Activity

public class HelloAndroidActivity extends Activity {

    /**
     * Called when the activity is first created.
     * @param savedInstanceState If the activity is being re-initialized after
     * previously being shut down then this Bundle contains the data it most
     * recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getActionBar().setTitle("Test");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.action_bar_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);


        if (R.id.locate == item.getItemId()) {

            final MenuItem menuItem = item.setActionView(R.layout.inderterminate_progress);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    SystemClock.sleep(3000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            menuItem.setActionView(null);
                        }
                    });
                }
            }).start();
        }

        return true;
    }
}

解决方案

It seems that explicitly defining the style and adding a 4dip padding in the root of the layout to inflate as shown below solves this issue

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingLeft="4dip"
    android:paddingRight="4dip"
    style="?android:attr/actionButtonStyle" />

This seems to be used in the android stylesheet here

这篇关于Android的动作条setActionView布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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