相对布局中的颜色问题onFocus / onClick

时间:2018-06-04 13:11:36

标签: android android-layout android-studio android-relativelayout

我的布局样式存在一些问题,实际上在Android 5.1.1上它工作正常,当我按下RelativeLayout时,它上面的文字会改变颜色,所以它看起来像是'选择了它,看起来更好。

在Android 7.0中,当RelativeLayout处于onClick或onFocus时,它会变成一种可怕的橙色,所以我觉得它看起来像在Android 5.1.1中...... enter image description here

enter image description here

以下是此视图中的xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
    tools:context=".MainActivity"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_marginTop="0dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="UselessLeaf,UselessParent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                tools:ignore="InefficientWeight,ObsoleteLayoutParam,UselessLeaf"
                android:baselineAligned="false">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/rellay_cassa"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_weight="0.65"
                    android:background="@color/blueLight"
                    android:clickable="true"
                    android:foreground="?attr/selectableItemBackground"
                    android:focusable="true"
                    tools:ignore="NestedWeights,ObsoleteLayoutParam">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        tools:ignore="UselessParent">

                        <ImageView
                            android:id="@+id/imgCassa"
                            android:layout_width="60dp"
                            android:layout_height="60dp"
                            android:layout_centerHorizontal="true"
                            android:adjustViewBounds="true"
                            android:padding="8dp"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_cassa"
                            android:tint="@color/colorWhite"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/imgCassa"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="5dp"
                            android:text="CASSA"
                            android:textSize="14sp"
                            tools:ignore="HardcodedText" />

                    </RelativeLayout>

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/rellay_tasti"
                    android:layout_weight="0.55"
                    android:clickable="true"
                    android:background="@color/blueDark"
                    android:foreground="?attr/selectableItemBackground"
                    android:focusable="true">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        tools:ignore="UselessParent">

                        <ImageView
                            android:id="@+id/imgTasti"
                            android:layout_width="60dp"
                            android:layout_height="60dp"
                            android:layout_centerHorizontal="true"
                            android:adjustViewBounds="true"
                            android:padding="8dp"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_tasti"
                            android:tint="@color/colorWhite"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/imgTasti"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="5dp"
                            android:text="AGGIUNGI TASTI"
                            android:textSize="14sp"
                            tools:ignore="HardcodedText" />

                    </RelativeLayout>

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:layout_weight="1"
                android:baselineAligned="false"
                tools:ignore="ObsoleteLayoutParam">

                <RelativeLayout
                    android:id="@+id/rellay_articoli"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.55"
                    android:background="@color/blueDark"
                    android:clickable="true"
                    android:foreground="?attr/selectableItemBackground"
                    android:focusable="true"
                    tools:ignore="InefficientWeight,NestedWeights">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    tools:ignore="ObsoleteLayoutParam,UselessParent">

                    <ImageView
                        android:id="@+id/imgArticoli"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_centerHorizontal="true"
                        android:adjustViewBounds="true"
                        android:padding="8dp"
                        android:scaleType="centerInside"
                        android:src="@drawable/ic_articoli"
                        android:tint="@color/colorWhite"
                        tools:ignore="ContentDescription" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/imgArticoli"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="5dp"
                        android:text="AGGIUNGI ARTICOLI"
                        android:textSize="14sp"
                        tools:ignore="HardcodedText" />

                </RelativeLayout>

             </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/rellay_clienti"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/blueLight"
                    android:layout_weight="0.55"
                    android:clickable="true"
                    android:foreground="?attr/selectableItemBackground"
                    android:focusable="true">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        tools:ignore="UselessParent">

                        <ImageView
                            android:id="@+id/imgClienti"
                            android:layout_width="60dp"
                            android:layout_height="60dp"
                            android:layout_centerHorizontal="true"
                            android:adjustViewBounds="true"
                            android:padding="8dp"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_clienti"
                            android:tint="@color/colorWhite"
                            tools:ignore="ContentDescription" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/imgClienti"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="5dp"
                            android:text="AGGIUNGI CLIENTI"
                            android:textSize="14sp"
                            tools:ignore="HardcodedText" />

                    </RelativeLayout>

                </RelativeLayout>

          </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="0dp"
        android:layout_weight="1"
        tools:ignore="ObsoleteLayoutParam"
        android:baselineAligned="false">

        <RelativeLayout
            android:id="@+id/rellay_settings"
            android:layout_width="347dp"
            android:layout_height="match_parent"
            android:layout_weight="0.65"
            android:background="@color/blueLight"
            android:clickable="true"
            android:foreground="?attr/selectableItemBackground"
            tools:ignore="InefficientWeight,NestedWeights,UselessParent"
            android:focusable="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true">

                <ImageView
                    android:id="@+id/imgSettings"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:padding="8dp"
                    android:scaleType="centerInside"
                    android:src="@drawable/ic_settings"
                    android:tint="@color/colorWhite"
                    tools:ignore="ContentDescription" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/imgSettings"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="IMPOSTAZIONI"
                    android:textSize="14sp"
                    tools:ignore="HardcodedText" />

            </RelativeLayout>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/rellay_exit"
            android:layout_width="331dp"
            android:layout_height="match_parent"
            android:layout_weight="0.55"
            android:background="@color/red"
            android:clickable="true"
            android:foreground="?attr/selectableItemBackground"
            android:focusable="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                tools:ignore="UselessParent">

                <ImageView
                    android:id="@+id/imgExit"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:padding="8dp"
                    android:scaleType="centerInside"
                    android:src="@drawable/ic_exit"
                    android:tint="@color/colorWhite"
                    tools:ignore="ContentDescription" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/imgExit"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="USCITA"
                    android:textSize="14sp"
                    tools:ignore="HardcodedText" />

            </RelativeLayout>

        </RelativeLayout>

     </LinearLayout>

  </LinearLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

通过java

应用焦点颜色
 int[][] states = new int[][] {
        new int[] { android.R.attr.state_pressed}, // pressed
        new int[] { android.R.attr.state_focused}, // focused
        new int[] { android.R.attr.state_enabled} // enabled
    };



     int[] colors = new int[] {
            Color.parseColor("#008000"), // green
            Color.parseColor("#0000FF"), // blue
            Color.parseColor("#FF0000")  // red
        };

        ColorStateList list = new ColorStateList(states, colors);
        textView.setTextColor(list);        
        textView.setClickable(true);
        textView.setFocusableInTouchMode(true);