0

嗨,我是 android 新手,我想更改我的工具栏和文本背景颜色,使用调色板颜色选择器从图像中动态选择颜色,请任何人帮助我如何更改我的工具栏颜色我有来自 json 对象的多个图像如何更改工具栏和文本来自 JSON 的背景,我想在单击时更改每个项目的工具栏颜色,请任何人帮助我如何在我的代码下方获得这个

手段磨损.java

int pos = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_means_wear);

    image = (ImageView)findViewById(R.id.imageView);
    tv1 = (TextView)findViewById(R.id.textView2);

    tv3 = (TextView)findViewById(R.id.textView3);
    tv4 = (TextView)findViewById(R.id.textView6);
    tv5 = (TextView)findViewById(R.id.textView5);
    tex1 = (TextView)findViewById(R.id.textView1);
    text2 = (TextView)findViewById(R.id.textView4);

    btn = (Button)findViewById(R.id.button);
    pos = Integer.parseInt((getIntent().getExtras()).getString("pos"));

    tv1.setText(Women_clothing.gridData.get(pos).getDocumentName());

    tv3.setText(Women_clothing.gridData.get(pos).getDocumentContent());
    tv4.setText(Women_clothing.gridData.get(pos).getOffer());
    tv5.setText(Women_clothing.gridData.get(pos).getAddress());

    Picasso.with(getApplicationContext()).load((Women_clothing.gridData.get(pos)).getDocumentFile()).into(image);
}
4

1 回答 1

1

试试下面这个

public static void colorizeToolbar(Toolbar toolbarView, int toolbarIconsColor, Activity activity) {
    final PorterDuffColorFilter colorFilter
            = new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.MULTIPLY);

    for(int i = 0; i < toolbarView.getChildCount(); i++) {
        final View v = toolbarView.getChildAt(i);

        //Step 1 : Changing the color of back button (or open drawer button).
        if(v instanceof ImageButton) {
            //Action Bar back button
            ((ImageButton)v).getDrawable().setColorFilter(colorFilter);
        }

        if(v instanceof ActionMenuView) {
            for(int j = 0; j < ((ActionMenuView)v).getChildCount(); j++) {

                //Step 2: Changing the color of any ActionMenuViews - icons that
                //are not back button, nor text, nor overflow menu icon.
                final View innerView = ((ActionMenuView)v).getChildAt(j);

                if(innerView instanceof ActionMenuItemView) {
                    int drawablesCount = ((ActionMenuItemView)innerView).getCompoundDrawables().length;
                    for(int k = 0; k < drawablesCount; k++) {
                        if(((ActionMenuItemView)innerView).getCompoundDrawables()[k] != null) {
                            final int finalK = k;

                            //Important to set the color filter in seperate thread, 
                            //by adding it to the message queue
                            //Won't work otherwise.
                            innerView.post(new Runnable() {
                                @Override
                                public void run() {
                                    ((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
                                }
                            });
                        }
                    }
                }
            }
        }

        //Step 3: Changing the color of title and subtitle.
        toolbarView.setTitleTextColor(toolbarIconsColor);
        toolbarView.setSubtitleTextColor(toolbarIconsColor);

        //Step 4: Changing the color of the Overflow Menu icon.
        setOverflowButtonColor(activity, colorFilter);
    }
}

请参阅此示例 1 示例 2

于 2016-02-22T08:33:56.970 回答