xpol
V2EX  ›  Android

请 Android 大咖帮忙,下 EditText 控件被 ActionBar 改变位置问题!

  •  
  •   xpol · Nov 18, 2015 · 12933 views
    This topic created in 3835 days ago, the information mentioned may be changed or developed.

    我在 Cocos2d 游戏中使用 Android EditText 控件。

    其他都很正常,但是在我选中 EditBox 中的文字的时候, "Select Cut Copy Paste" 动作条出现在屏幕上方,同时我的 EditBox 文字位置下移,如下图:

    compare

    我肯定不需要这个的结果,我想要如下,当我选中的时候,虽然上方出现动作条,但是我的 EditBox 应该保持再原来的位置,比如下图这样:

    enter image description here

    这里是我使用的 EditBox 设置位置的代码:

    public void setEditBoxViewRect(int left, int top, int maxWidth, int maxHeight) {
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                                                                                FrameLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.leftMargin = left;
            layoutParams.topMargin = top;
            layoutParams.width = maxWidth;
            layoutParams.height = maxHeight;
            layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
            this.setLayoutParams(layoutParams);
        }
    

    完整代码见 GitHub: cocos2d/cocos2d-x Cocos2dxEditBox.java

    当前我的 Workaround 是这样的,关掉 ActionBar :

    edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
    
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                }
    
                public void onDestroyActionMode(ActionMode mode) {
    
                }
    
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    return false;
                }
    
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    return false;
                }
            });
    

    我猜问题的原因是不是因为我设置的是绝对坐标,没有使用布局的原因。当然在游戏里希望直接的位置设定。那么我能不能在入上面的 workaround 的 onCreateActionMode 获得 ActionBar 的高度,然后临时调整 EditBox 的位置呢?或者一般大家遇到这样情况的时候是怎么解决的呢?

    2 replies    2015-11-19 22:17:36 +08:00
    cppgohan
        1
    cppgohan  
       Nov 18, 2015   ❤️ 1
    主题 style 里加上
    ```
    <item name="android:windowActionModeOverlay">true</item>
    ```
    xpol
        2
    xpol  
    OP
       Nov 19, 2015
    @cppgohan 感谢。
    我之前惜 xml 没写对 AndroidManifest.xml 也没有设置。

    下面是最终解决的相对完整的文件:

    res/style.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <style name="ActionModeOverlay" parent="android:Theme.Black.NoTitleBar">
    <item name="android:windowActionModeOverlay">true</item>
    </style>
    </resources>


    AndroidManifest.xml

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

    ...

    <application android:label="@string/app_name"
    android:icon="@drawable/icon"
    android:allowBackup="false">

    <activity android:name="my.application.AppActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:configChanges="orientation"
    android:theme="@style/ActionModeOverlay">
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3259 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 11:06 · PVG 19:06 · LAX 04:06 · JFK 07:06
    ♥ Do have faith in what you're doing.