如何更改ZoomButtonsController中按钮触摸的颜色

时间:2017-03-17 13:51:49

标签: android button controller zoom

我正在我的应用中实现ZoomButtonsContainer。它的效果很好,但按下时+和 - 按钮的颜色是一种黄赭色。我想改变它以反映我的应用程序的颜色。我使用的是Theme.AppCompat.Light.DarkActionBar。

我已经尝试在我的活动主题中覆盖了一些样式属性但没有任何效果。我已尝试过以下内容:

 <item name="android:colorPressedHighlight">@color/plum</item>
 <item name="android:colorActivatedHighlight">@color/plum</item>

任何解决方案?

1 个答案:

答案 0 :(得分:0)

我已经通过更彻底的解决方案绕过它来解决这个问题,这个解决方案甚至更好。我已经在ZoomButtonsController中用我自己的布局完全替换了按钮。如果这对任何人都有用,我会在下面显示代码。

步骤:

  1. 以我所需的样式创建按钮的布局,但参考使用的默认布局。
  2. 为按钮创建背景,以显示启用/按下/默认所需的颜色。
  3. 使用此布局创建My_ZoomControls类,根据需要从默认的ZoomControls中获取代码。
  4. 创建一个从ZoomButtonsController扩展但引用My_ZoomControls的My_ZoomButtonsController。
  5. 通过切换默认布局并将其替换为自定义布局进行初始化。
  6. 包含从窗口分离时调用setVisible(false)的代码,以及根据ZoomButtonsController文档更改的可见性。
  7. 请注意,您需要注意控制器&#34; ownerView&#34;每个解决方案here
  8. zoom_contols.xml     

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/popup_player_background"
        android:orientation="horizontal">
    
        <ZoomButton
            android:id="@+id/zoomOut"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/zoom_controls_item_background"
            android:contentDescription="@string/app_name"
            android:gravity="center"
            android:paddingEnd="20dp"
            android:paddingLeft="10dp"
            android:paddingRight="20dp"
            android:paddingStart="10dp"
            android:src="@drawable/minus_circled"
            android:tint="@color/plum"/>
    
        <ZoomButton
            android:id="@+id/zoomIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/zoom_controls_item_background"
            android:contentDescription="@string/app_name"
            android:paddingEnd="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="10dp"
            android:paddingStart="20dp"
            android:src="@drawable/plus_circled"
            android:tint="@color/plum"/>
    </LinearLayout>
    

    zoom_contols_item_background

    <?xml version="1.0" encoding="utf-8"?>
    

    <item android:state_enabled="false">
        <shape
            android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/plum"/>
            <solid android:color="@color/gray"/>
            <padding android:bottom="2dp"
                     android:left="2dp"
                     android:right="2dp"
                     android:top="2dp"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    
    <item android:state_pressed="true">
        <shape
            android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/plum"/>
            <solid android:color="@color/plum_light"/>
            <padding android:bottom="2dp"
                     android:left="2dp"
                     android:right="2dp"
                     android:top="2dp"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    
    <item>
        <shape
            android:shape="rectangle">
            <stroke android:width="1dp" android:color="@color/transparent"/>
            <solid android:color="@color/transparent"/>
            <padding android:bottom="2dp"
                     android:left="2dp"
                     android:right="2dp"
                     android:top="2dp"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    

    My_ZoomControls

    public class My_ZoomControls extends LinearLayout {
    
    private final ZoomButton mZoomInButton;
    private final ZoomButton mZoomOutButton;
    
    public My_ZoomControls(Context context) {
        super(context);
        setFocusable(false);
    
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.zoom_controls, this, true);
    
        mZoomInButton = (ZoomButton) findViewById(R.id.zoomIn);
        mZoomOutButton = (ZoomButton) findViewById(R.id.zoomOut);
    }
    
    public void setOnZoomInClickListener(OnClickListener listener) {
        mZoomInButton.setOnClickListener(listener);
    }
    
    public void setOnZoomOutClickListener(OnClickListener listener) {
        mZoomOutButton.setOnClickListener(listener);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }
    
    public void setIsZoomInEnabled(boolean isEnabled) {
        mZoomInButton.setEnabled(isEnabled);
    }
    
    public void setIsZoomOutEnabled(boolean isEnabled) {
        mZoomOutButton.setEnabled(isEnabled);
    }
    

    }

    My_ZoomButtonsController

    public class My_ZoomButtonsController extends ZoomButtonsController {
    
    private My_ZoomControls mControls;
    private OnZoomListener mZoomListener;
    private View.OnAttachStateChangeListener mAttachStateChangeListener;
    
    public My_ZoomButtonsController(View ownerView) {
        super(ownerView);
    }
    
    public void setZoomControls(My_ZoomControls mControls) {
        this.mControls = mControls;
        this.mControls.setOnZoomInClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mZoomListener != null) mZoomListener.onZoom(true);
            }
        });
        this.mControls.setOnZoomOutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mZoomListener != null) mZoomListener.onZoom(false);
            }
        });
        this.mControls.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                if (mAttachStateChangeListener != null) mAttachStateChangeListener.onViewAttachedToWindow(v);
            }
    
            @Override
            public void onViewDetachedFromWindow(View v) {
                if (mAttachStateChangeListener != null) mAttachStateChangeListener.onViewDetachedFromWindow(v);
            }
        });
    }
    
    public void setOnZoomListener(OnZoomListener listener) {
        mZoomListener = listener;
    }
    
    public void setOnAttachStateChangeListener(View.OnAttachStateChangeListener listener) {
        mAttachStateChangeListener = listener;
    }
    
    @Override
    public void setZoomOutEnabled(boolean enabled) {
        mControls.setIsZoomOutEnabled(enabled);
    }
    
    @Override
    public void setZoomInEnabled(boolean enabled) {
        mControls.setIsZoomInEnabled(enabled);
    }
    

    }

    实现。 zoomInGrid是我放置应用程序特定代码以放大的地方。

    private void setZoomButtonsController() {
        pZoomButtonsController = new My_ZoomButtonsController(pBtnSort);
        ViewGroup container = pZoomButtonsController.getContainer();
        View controls = pZoomButtonsController.getZoomControls();
        ViewGroup.LayoutParams params = controls.getLayoutParams();
        container.removeView(controls);
        My_ZoomControls layout = new My_ZoomControls(My_Application_Base.getInstance().getApplicationContext());
        container.addView(layout, params);
        pZoomButtonsController.setZoomControls(layout);
        pZoomButtonsController.setOnZoomListener(new ZoomButtonsController.OnZoomListener() {
    
            @Override
            public void onZoom(boolean zoomIn) {
                zoomInGrid(zoomIn);
            }
    
            @Override
            public void onVisibilityChanged(boolean visible) {
                if ((!visible) && (pZoomButtonsController != null)) {
                    pZoomButtonsController.setVisible(false);
                }
            }
        });
        pZoomButtonsController.setOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
            }
    
            @Override
            public void onViewDetachedFromWindow(View v) {
                if (pZoomButtonsController != null) {
                    pZoomButtonsController.setVisible(false);
                }
            }
        });
    }