【问题标题】:Remove "Done" button of ActionMode删除 ActionMode 的“完成”按钮
【发布时间】:2013-02-04 13:46:37
【问题描述】:

0我在我的应用上使用 startActionMode(ActionMode)。

默认情况下,它会在栏上添加一个“完成”按钮,我想将其删除。

另外,如果有办法改变它的文本,我也想知道,导致与“完成”不同的描述可以使操作与它所做的行为相对应。

【问题讨论】:

  • 提供图片让您更清楚您在说什么。
  • 恕我直言,你想要的是无效的设计。根据定义,用户可以关闭操作模式,例如按下 BACK 按钮。默认情况下,动作模式应该是非破坏性的:激活动作模式,然后按 BACK(或点击完成)不会导致用户数据发生任何变化。
  • 嗯.. 这也是真的。那么有一种方法可以改变它的文本,这样用户就不会感到困惑了?因为有时“取消”是比“完成”更好的描述。泰!
  • @MarcosVasconcelos:也许通过主题。 API 中没有任何内容。
  • 好吧,我试图找到一些东西,但有办法自定义样式,而不是文本本身。但即使有,我也需要完成一些和其他的取消。

标签: android android-actionbar android-actionmode


【解决方案1】:

我同意 @CommonsWare 的观点,即隐藏 完成 按钮的设计无效。

但是,有些客户希望删除此按钮,我可以理解复选标记可能会导致用户混淆,因为在某些情况下它实际上没有任何作用。

那么,这里是如何删除带有样式的按钮:

<style name="AppTheme" parent="android:Theme.Holo">
    <item name="android:actionModeCloseButtonStyle">@style/NoCloseButton</item>
</style>

<style name="NoCloseButton" parent="@android:style/Widget.ActionButton.CloseMode">
    <item name="android:visibility">gone</item>
</style>

【讨论】:

  • API 11+ 的绝佳解决方案,但有没有办法在 API 8+ 中做到这一点?我使用 ActionBarSherlok 有没有办法隐藏它? ActionModeCloseButtonStyle 需要 API 11+
  • @Cilenco: actionModeCloseButtonStyle(没有android:)应该适用于ABS。
  • 你说得对,谢谢我添加了一个适用于 ABS vor Android API8+ 的答案
  • 它可以工作,但是,如果我膨胀自定义布局,它不会占据整个宽度。这是唯一的问题。
  • @MatthiasRobbers 我已经为 CAB 应用了上述样式,NoCloseButton 的可见性消失了,它仍然占用空间并且不允许添加其他图标
【解决方案2】:

如果您使用的是 ActionBarSherlock,您可以使用这种样式隐藏“完成”按钮:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="NoCloseButton" parent="@style/Widget.Sherlock.ActionButton.CloseMode">
        <item name="android:visibility">gone</item>
    </style>

    <style name="AppBaseTheme" parent="Theme.Sherlock.Light.DarkActionBar">
        <item name="actionModeCloseButtonStyle">@style/NoCloseButton</item>
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">

    </style>
</resources>

请确保将此样式放在每个样式目录中(values、value-v11、values-v14)。之后,您可以使用以下代码创建自定义菜单:

LayoutInflater inflater = LayoutInflater.from(getSherlockActivity());
View actionView = inflater.inflate(R.layout.actionmode, null);

ActionMode am = getSherlockActivity().startActionMode(mActionModeCallback);
am.setCustomView(actionView);

【讨论】:

  • 但是这里的actionView并没有占据整个屏幕宽度。
  • 是的。但是这里的 actionView 并没有占据整个屏幕宽度
  • 有人找到屏幕宽度的解决方案吗?
【解决方案3】:

即使我被困住了,我也找到了解决它的方法。

int doneButtonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
LinearLayout layout = (LinearLayout) findViewById(doneButtonId);
TextView doneview = (TextView) layout.getChildAt(1);
doneview.setText("");

希望这会有所帮助!

【讨论】:

  • 这是一个不可靠的解决方案,因为它假定实施细节可能会发生变化。
  • 不工作我试过这个我得到空指针异常,我应该什么时候调用这个方法,我正在使用片段
  • 空指针异常是因为ActionMode没有完成初始化。尝试在onCreateActionMode()方法中使用view.postDelayed(Runnable action, long delayMillis)延迟一段时间!!!
【解决方案4】:

感谢Matthias Robbers,它有效。但我更喜欢使用“隐形”:

<style name="NoCloseButtonActionModeStyle">
   <item name="android:visibility">invisible</item>
</style>

所以 ActionMode 中的自定义视图位置不会缩进到父级左侧。

【讨论】:

    【解决方案5】:

    有人找到屏幕宽度的解决方案吗? – Mayur R. Amipara 2015 年 6 月 5 日 10:27

    覆盖自定义视图的 onMeasure,将其宽度设置为屏幕宽度。并在 onLayout 中重新布局您的子视图。

    它对我有用。

    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.DisplayMetrics;
    import android.view.View;
    import android.widget.RelativeLayout;
    
    import com.android.gallery3d.R;
    
    public class SelectActionModeCustomView extends RelativeLayout {
    
        private View mLeftButton;
        private View mRightButton;
    
        private int mScreenWidth;
    
        private static final String TAG = "SelectActionView";
    
        public SelectActionModeCustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            mScreenWidth = metrics.widthPixels;
        }
    
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            mLeftButton = findViewById(R.id.cancel);
            mRightButton = findViewById(R.id.select_action);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            this.setMeasuredDimension(mScreenWidth, this.getMeasuredHeight());
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            int childLeft = 0;
            int childTop = 0;
    
            childLeft = 0;
            childTop = ((b - t) - mLeftButton.getMeasuredHeight()) / 2;
            mLeftButton.layout(childLeft, childTop, 
                    childLeft + mLeftButton.getMeasuredWidth(), 
                    childTop + mLeftButton.getMeasuredHeight());
    
            childLeft = (r - l) - mRightButton.getMeasuredWidth();
            childTop = ((b - t) - mRightButton.getMeasuredHeight()) / 2;
            mRightButton.layout(childLeft, childTop, 
                    childLeft + mRightButton.getMeasuredWidth(), 
                    childTop + mRightButton.getMeasuredHeight());
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      相关资源
      最近更新 更多