This sh开发者_如何学运维ape is the backround of a linearLayout, how can I make any TextViews within the LinearLayout change their text color to white during the statePressed =true of the parent selector? Also see my previous question to see more of the code: Shape does not show
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#AA000000" />
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
<gradient
android:startColor="#6633cc"
android:endColor="#00ccff"
android:angle="270" />
</shape>
Pressed state is propagated to all child views of a layout. You can declare a state list of colors similarly to how you declare a state list of drawables. If you set a state list color on text within a parent that becomes pressed, the pressed state will apply to the text color as well.
EDIT: The quoted information is partially incorrect and was edited in by someone else:
USE: android:duplicateParentState="true" for the TextView, also USE: android:color="@color/myNewColor"
This XML color file is placed in the res/color directory of a project.
Here's an example from AOSP, used for default text: https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/color/primary_text_dark.xml
EDIT: You edited my answer with incorrect information. Pressed state is propagated regardless of the android:duplicateParentState
setting.
View.java, the setPressed
method invokes dispatchSetPressed
:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/View.java#8521
ViewGroup.java, the overridden dispatchSetPressed
method sets all child views pressed:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/ViewGroup.java#3833
<TextView
android:textColor="#ffffff"
/>
精彩评论