View.setBackgroundResource()未设置正确的背景资源 [英] View.setBackgroundResource() is not setting the correct background resource

查看:81
本文介绍了View.setBackgroundResource()未设置正确的背景资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编码一个设置销".碎片第二次,它不起作用.一切正常,直到我使用 switch 块和 View.OnTouchListener .我试图将按钮的背景资源在按下时更改为其他可绘制对象,然后在放开手指时将其更改为其他可绘制对象.

我的问题是后台资源根本没有改变.

用于布局的XML:

 <?xml version ="1.0"encoding ="utf-8"?< androidx.constraintlayout.widget.ConstraintLayoutxmlns:android ="http://schemas.android.com/apk/res/android"xmlns:app =" http://schemas.android.com/apk/res-autoxmlns:tools ="http://schemas.android.com/tools"android:background =" @ color/tertiary_dark"android:layout_width =" match_parent"android:layout_height =" match_parent"android:theme =" @ style/Theme.Design.NoActionBar"><查看android:id ="@@ id/divider3"android:layout_width =" 364.059dp"android:layout_height =" 2dp"android:background =" @ color/secondary_accent"app:layout_constraintEnd_toEndOf ="parent"app:layout_constraintHorizo​​ntal_bias ="0.5"app:layout_constraintStart_toStartOf =" parent"app:layout_constraintTop_toBottomOf =" @ + id/tableLayout"/>< TableLayoutandroid:id ="@@ id/tableLayout"android:layout_width =" wrap_content"android:layout_height =" wrap_content"android:layout_marginBottom =" 16dp"app:layout_constraintBottom_toBottomOf =" parent"app:layout_constraintEnd_toEndOf ="parent"app:layout_constraintStart_toStartOf =" parent"><表格行android:layout_width =" match_parent"android:layout_height =" match_parent"><按钮android:id ="@@ id/pin7"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:background =" @ drawable/button_1_background_idle"android:text ="7";android:textColor =" @ color/primary_light"android:textSize ="22sp"/><按钮android:id ="@@ id/pin8"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text =" 8"android:textColor =" @ color/primary_light"android:textSize ="22sp"/><按钮android:id ="@@ id/pin9"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="9";android:textColor =" @ color/primary_light"android:textSize ="22sp"/></TableRow><表格行android:layout_width =" match_parent"android:layout_height =" match_parent"><按钮android:id ="@@ id/pin4"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="4";android:textColor =" @ color/primary_light"android:textSize ="22sp"/><按钮android:id ="@@ id/pin5"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="5"android:textColor =" @ color/primary_light"android:textSize ="22sp"/><按钮android:id ="@@ id/pin6"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="6";android:textColor =" @ color/primary_light"android:textSize ="22sp"/></TableRow><表格行android:layout_width =" match_parent"android:layout_height =" match_parent"><按钮android:id ="@@ id/pin1"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="1";android:textColor =" @ color/primary_light"android:textSize ="22sp"/><按钮android:id ="@@ id/pin2"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="2"android:textColor =" @ color/primary_light"android:textSize ="22sp"/><按钮android:id ="@@ id/pin3"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="3";android:textColor =" @ color/primary_light"android:textSize ="22sp"/></TableRow><表格行android:layout_width =" match_parent"android:layout_height =" match_parent"><按钮android:id ="@@ id/pina"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="A";android:textColor =" @ color/primary_light"android:textSize ="22sp"/><按钮android:id ="@@ id/pin0&";android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="0";android:textColor =" @ color/primary_light"android:textSize ="22sp"/><按钮android:id ="@@ id/pinb"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text ="B";android:textColor =" @ color/primary_light"android:textSize ="22sp"/></TableRow></TableLayout><查看android:id ="@@ id/view"android:layout_width ="364.059dp"android:layout_height =" 75dp"android:layout_marginTop =" 104dp"android:background =" @ color/primary_dark"app:layout_constraintEnd_toEndOf ="parent"app:layout_constraintHorizo​​ntal_bias ="0.489"app:layout_constraintStart_toStartOf =" parent"app:layout_constraintTop_toTopOf =" parent"/>< TextViewandroid:id =" @ + id/textView3"android:layout_width =" wrap_content"android:layout_height =" wrap_content"android:fontFamily =" @ font/slatebook"android:text =" 1234567890ab"android:textColor =" @ color/primary_light"android:textSize =" 30sp"app:layout_constraintBottom_toBottomOf ="@@ id/view"app:layout_constraintEnd_toEndOf ="@@ id/view"app:layout_constraintStart_toStartOf ="@@ id/view"app:layout_constraintTop_toTopOf =" @ + id/view"/><查看android:id ="@@ id/divider"android:layout_width =" 364.059dp"android:layout_height =" 2dp"android:background =" @ color/secondary_accent"app:layout_constraintEnd_toEndOf ="parent"app:layout_constraintStart_toStartOf =" parent"app:layout_constraintTop_toBottomOf ="@@ id/view"/></androidx.constraintlayout.widget.ConstraintLayout> 

java类的代码:

  package com.example.portal;导入android.annotation.SuppressLint;导入android.graphics.Color;导入android.os.Bundle;导入android.system.StructUtsname;导入android.view.LayoutInflater;导入android.view.MotionEvent;导入android.view.View;导入android.view.ViewGroup;导入android.widget.Button;导入androidx.annotation.NonNull;导入androidx.core.content.ContextCompat;导入androidx.fragment.app.Fragment;导入androidx.navigation.fragment.NavHostFragment;导入java.util.ArrayList;公共类FirstFragment扩展Fragment {@Override公共视图onCreateView(LayoutInflater充气机,ViewGroup容器,捆绑saveInstanceState){//扩充此片段的布局return inflater.inflate(R.layout.login_layout,container,false);}@SuppressLint("ClickableViewAccessibility")public void onViewCreated(@NonNull View view,Bundle savedInstanceState)捆绑{super.onViewCreated(view,savedInstanceState);加载();}@SuppressLint("ClickableViewAccessibility")私有无效负载(){ArrayList< Button>pinbtns = new ArrayList<>();ArrayList< Character>vals = new ArrayList<>();pinbtns.add((Button)getActivity().findViewById(R.id.pin0));pinbtns.add((Button)getActivity().findViewById(R.id.pin1));pinbtns.add((Button)getActivity().findViewById(R.id.pin2));pinbtns.add((Button)getActivity().findViewById(R.id.pin3));pinbtns.add((Button)getActivity().findViewById(R.id.pin4));pinbtns.add((Button)getActivity().findViewById(R.id.pin5));pinbtns.add((Button)getActivity().findViewById(R.id.pin6));pinbtns.add((Button)getActivity().findViewById(R.id.pin7));pinbtns.add((Button)getActivity().findViewById(R.id.pin8));pinbtns.add((Button)getActivity().findViewById(R.id.pin9));pinbtns.add((Button)getActivity().findViewById(R.id.pina));pinbtns.add((Button)getActivity().findViewById(R.id.pinb));vals.add('0');vals.add('1');vals.add('2');vals.add('3');vals.add('4');vals.add('5');vals.add('6');vals.add('7');vals.add('8');vals.add('9');vals.add('a');vals.add('b');for(int r = 0; r< pinbtns.size(); r ++){按钮btn = pinbtns.get(r);btn.setOnTouchListener(new View.OnTouchListener(){@Overridepublic boolean onTouch(查看v,MotionEvent事件){开关(event.getAction()){案例MotionEvent.ACTION_DOWN:btn.setBackgroundResource(R.drawable.button_1_background_pressed);System.out.println("down");返回true;案例MotionEvent.ACTION_UP:线程t =新线程(()-> {尝试 {Thread.sleep(50);} catch(Exception ig){ig.printStackTrace();}getActivity().runOnUiThread(()-> {btn.setBackgroundResource(R.drawable.button_1_background_idle);});});t.start();System.out.println("up");返回true;}返回false;}});}}} 

可绘制对象的代码:

 <?xml version ="1.0"encoding ="utf-8"?< shape android:shape ="矩形";xmlns:android ="http://schemas.android.com/apk/res/android"><固体android:color =" @ color/primary_dark"/></形状><?xml version ="1.0"encoding ="utf-8"?< shape android:shape ="矩形";xmlns:android ="http://schemas.android.com/apk/res/android"><固体android:color =" @ color/primary_accent"/></形状> 

还有很好的颜色资源文件.

 <?xml version ="1.0"encoding ="utf-8"?<资源><颜色名称="primary_light"> #dcdcdc</color><颜色名称="primary_dark">#1c1d1f</color><颜色名称="secondary_dark">#151515</color><颜色名称="tertiary_dark">#323335</color><颜色名称="secondary_light">#9ea09f</color><颜色名称="primary_accent">#c41442</color><颜色名称="secondary_accent">#711930</color></resources> 

再澄清一次.当我按下按钮(例如pin7)时,系统会打印"down"(向下)到控制台,但背景不会改变一位.当我释放时,我会起来".在控制台中.因此,除了背景之外,它都按预期工作.

我尝试过将 setBacgroundResource 更改为 setBackroundColor ,并获得相同的结果.

解决方案

我花了2个小时,试图找出为什么 setBackgroundResource()无法正常工作.我在形状上添加了一些修改,并且注意到仅对颜色进行了更改,颜色除外.因此,我创建了一种消除色调的样式,将 backgroundTint 放在 @null 上,一切正常./p>

这是我添加的样式

 < style name =" NotTintedButton"parent ="Widget.AppCompat.Button.Colored".< item name ="backgroundTint"> @ null</item></style> 

所有您需要做的就是在每个按钮上应用此样式.例如:

 <按钮style ="@ style/NotTintedButton"android:id ="@@ id/pin8"android:layout_width =" @ dimen/button_1_width"android:layout_height =" @ dimen/button_1_height"android:layout_margin =" @ dimen/button_1_margin"android:background =" @ color/primary_dark"android:fontFamily =" @ font/slatebook"android:padding ="0dp"android:text =" 8"android:textColor =" @ color/primary_light"android:textSize ="22sp"/> 

I am coding a "set pin" fragment for the second time and it's not working. Everything works great until I use switch blocks and View.OnTouchListener. I am trying to change the buttons background resource to a different drawable when pressed down and then back when finger is lifted.

My problem is that the background resource is not changing at all.

XML for layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/tertiary_dark"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.Design.NoActionBar"
>

<View
    android:id="@+id/divider3"
    android:layout_width="364.059dp"
    android:layout_height="2dp"
    android:background="@color/secondary_accent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tableLayout" />

<TableLayout
    android:id="@+id/tableLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <Button
            android:id="@+id/pin7"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:background="@drawable/button_1_background_idle"
            android:text="7"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin8"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="8"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin9"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="9"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/pin4"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="4"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin5"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="5"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin6"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="6"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/pin1"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="1"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin2"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="2"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin3"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="3"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/pina"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="A"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pin0"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="0"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

        <Button
            android:id="@+id/pinb"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="B"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

    </TableRow>
</TableLayout>

<View
    android:id="@+id/view"
    android:layout_width="364.059dp"
    android:layout_height="75dp"
    android:layout_marginTop="104dp"
    android:background="@color/primary_dark"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.489"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/slatebook"
    android:text="1234567890ab"
    android:textColor="@color/primary_light"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="@+id/view"
    app:layout_constraintEnd_toEndOf="@+id/view"
    app:layout_constraintStart_toStartOf="@+id/view"
    app:layout_constraintTop_toTopOf="@+id/view" />

<View
    android:id="@+id/divider"
    android:layout_width="364.059dp"
    android:layout_height="2dp"
    android:background="@color/secondary_accent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view" />

</androidx.constraintlayout.widget.ConstraintLayout>

Code for java class:

package com.example.portal;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.system.StructUtsname;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import java.util.ArrayList;

public class FirstFragment extends Fragment {

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.login_layout, container, false);
    }

    @SuppressLint("ClickableViewAccessibility")
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        load();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void load() {
        ArrayList<Button> pinbtns = new ArrayList<>();
        ArrayList<Character> vals = new ArrayList<>();

        pinbtns.add((Button) getActivity().findViewById(R.id.pin0));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin1));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin2));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin3));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin4));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin5));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin6));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin7));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin8));
        pinbtns.add((Button) getActivity().findViewById(R.id.pin9));
        pinbtns.add((Button) getActivity().findViewById(R.id.pina));
        pinbtns.add((Button) getActivity().findViewById(R.id.pinb));

        vals.add('0');
        vals.add('1');
        vals.add('2');
        vals.add('3');
        vals.add('4');
        vals.add('5');
        vals.add('6');
        vals.add('7');
        vals.add('8');
        vals.add('9');
        vals.add('a');
        vals.add('b');

        for (int r = 0; r < pinbtns.size(); r++) {
            Button btn = pinbtns.get(r);
            btn.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:

                            btn.setBackgroundResource(R.drawable.button_1_background_pressed);

                            System.out.println("down");

                            return true;
                        case MotionEvent.ACTION_UP:

                            Thread t = new Thread(() -> {
                                try {
                                    Thread.sleep(50);
                                } catch (Exception ig) {
                                    ig.printStackTrace();
                                }
                                getActivity().runOnUiThread(() -> {
                                    btn.setBackgroundResource(R.drawable.button_1_background_idle);
                                });
                            });
                            t.start();

                            System.out.println("up");

                            return true;
                    }

                    return false;
                }
            });

        }

    }
}

Code for drawables:

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

    <solid
        android:color="@color/primary_dark"/>

</shape>


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

    <solid
        android:color="@color/primary_accent"/>

</shape>

And the color resourcefile for good measure.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primary_light">#dcdcdc </color>
    <color name="primary_dark">#1c1d1f</color>
    <color name="secondary_dark">#151515</color>
    <color name="tertiary_dark">#323335</color>
    <color name="secondary_light">#9ea09f</color>
    <color name="primary_accent">#c41442</color>
    <color name="secondary_accent">#711930</color>
</resources>

Just to clarify one more time. When I press a button (for example pin7) the system prints "down" to the console but the background does not change one bit. When I release I get "up" in the console. So it works as intended except for the background.

I have tried changing the setBacgroundResource to setBackroundColor and same outcome.

解决方案

I have spent 2 hours and am trying to find out why the setBackgroundResource() is not working. I added some modification on the shape and I noticed that the change only made on the dimensions except the Color. So I created a style to eliminate the Tinte I put the backgroundTint on @null and everything works correctly.

here is the style I added

 <style name="NotTintedButton" parent="Widget.AppCompat.Button.Colored">
        <item name="backgroundTint">@null</item>
 </style>

And all you have to do is apply this style on each buttons. for example :

 <Button        
            style="@style/NotTintedButton"
            android:id="@+id/pin8"
            android:layout_width="@dimen/button_1_width"
            android:layout_height="@dimen/button_1_height"
            android:layout_margin="@dimen/button_1_margin"
            android:background="@color/primary_dark"
            android:fontFamily="@font/slatebook"
            android:padding="0dp"
            android:text="8"
            android:textColor="@color/primary_light"
            android:textSize="22sp" />

这篇关于View.setBackgroundResource()未设置正确的背景资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆