首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在相对布局中onFocus/onClick时的颜色问题

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

Stack Overflow用户
提问于 2018-06-04 21:11:36
回答 1查看 82关注 0票数 0

我的布局风格有一些问题,实际上在Android5.1.1上它工作得很好,当我按下RelativeLayout时,上面的文本会改变颜色,所以它看起来像是被选中了,而且看起来更好。

而在Android7.0中,当RelativeLayout是onClick或onFocus时,它会变成可怕的橙色,所以我想它看起来就像在Android5.1.1中一样……

下面是我在这个视图中的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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-04 21:19:34

通过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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50681479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档