Chegara tugmasi fonini o'zgartirish

Chegaralangan tugma yaratdim:



    

    

va

<button
        varoid:text="@null"
        varoid:stateListAnimator="@null"
        varoid:layout_width="50dp"
        varoid:layout_height="50dp"
        varoid:id="@+id/colorButton"
        varoid:background="@drawable/button_border" />

Now I change the color of the background programmatically. The problem is that the border gets removed as soon as I change the background. Is there a way to change to background color of the button va keeping the border?

1

6 javoblar

Buni ko'ring,

    Button colorButton = (Button) findViewById(R.id.colorButton);
    GradientDrawable background = (GradientDrawable) colorButton.getBackground();
    background.setColor(getResources().getColor(R.color.some_color));
3
qo'shib qo'ydi
Salomat bo'ling :)
qo'shib qo'ydi muallif user2025187, manba
Menga qazishni juda yaxshi yo'nalish berdim :) juda yaxshi!
qo'shib qo'ydi muallif LPVOID, manba
Jozibasi kabi ishlaydi. Rahmat!
qo'shib qo'ydi muallif Patricks, manba

Buni ko'ring,

    Button colorButton = (Button) findViewById(R.id.colorButton);
    GradientDrawable background = (GradientDrawable) colorButton.getBackground();
    background.setColor(getResources().getColor(R.color.some_color));
3
qo'shib qo'ydi
Salomat bo'ling :)
qo'shib qo'ydi muallif user2025187, manba
Menga qazishni juda yaxshi yo'nalish berdim :) juda yaxshi!
qo'shib qo'ydi muallif LPVOID, manba
Jozibasi kabi ishlaydi. Rahmat!
qo'shib qo'ydi muallif Patricks, manba

Quyidagi koddan foydalaning



    

    
 


Rangi o'zgartirish uchun quyidagi kodni dinamik ravishda foydalaning.

Drawable background = yourView.getBackground();
    if (background instanceof ShapeDrawable) {
       //cast to 'ShapeDrawable'
        ShapeDrawable shapeDrawable = (ShapeDrawable) background;
        shapeDrawable.getPaint().setColor(getResources().getColor(R.color.colorToSet));
    } else if (background instanceof GradientDrawable) {
       //cast to 'GradientDrawable'
        GradientDrawable gradientDrawable = (GradientDrawable) background;
        gradientDrawable.setColor(getResources().getColor(R.color.colorToSet));
    } else if (background instanceof ColorDrawable) {
       //alpha value may need to be set again after this call
        ColorDrawable colorDrawable = (ColorDrawable) background;
        colorDrawable.setColor(getResources().getColor(R.color.colorToSet));
    }
2
qo'shib qo'ydi
Muammo manba faylida butonning orqa rejasi chizilgan bilan o'rnatiladi va btn.setBackgroundColor (Color.parseColor ("# ff0000") bilan ushbu fonni dasturiy jihatdan o'zgartiraman); Shunday qilib, men qatordagi rangni shakli bilan almashtirishim kerak va buni qanday qilishni bilmayman
qo'shib qo'ydi muallif Patricks, manba

Mening nuqtai nazarimdan tortishish shart emas, faqat har qanday tartibni qo'shing tugmachasiga android: layout_margin = "1dp" va android: background = "# FF4081" RelativeLayout uchun. Endi sizgina butttoningizning fonini ni o'zgartiring.

  
            <button
                android:id="@+id/colorButton"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="1dp"
                android:background="@android:color/white"
                android:text="@null" />
        
1
qo'shib qo'ydi
Bu ham juda yaxshi yo'l. Men ilgari bu haqda o'ylab ko'rganman. Rahmat
qo'shib qo'ydi muallif Patricks, manba

Shu rasm yordamida yangi rangni qo'llash uchun DrawableCompat sinf.

DrawableCompat.setTintList(d,drawableTintColor);//d is drawable object and drawableTintColor is color you want to apply
0
qo'shib qo'ydi

Ehtimol, sinchkovlik bilan fonga mos yozuvlar olishingiz va undan keyin ba'zi bir rangni qo'llashingiz kerak.

    GradientDrawable gradientDrawable = (GradientDrawable) colorButton.getBackground();
    gradientDrawable.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC);

bu erda # FF0000 - siz ko'rsatmoqchi bo'lgan yangi rang. Shunday qilib chegaralar ko'tarilmaydi deb o'ylayman.

0
qo'shib qo'ydi