Yaw*_*war 54 android colors android-spinner

如何更改微调器右下角的小三角形颜色,如图所示?它现在显示默认的灰色.像这样

ton*_*oni 85
最好最简单的解决方案:
spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)
其他解决方案(感谢Simon)如果您不想更改所有Spinners:
Drawable spinnerDrawable = spinner.getBackground().getConstantState().newDrawable();
spinnerDrawable.setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
spinner.setBackground(spinnerDrawable);
}else{
spinner.setBackgroundDrawable(spinnerDrawable);
}
Run Code Online (Sandbox Code Playgroud)
Lui*_*uis 40
从Lollipop开始,你可以在xml上设置背景色调,
android:backgroundTint="@color/my_color"
Run Code Online (Sandbox Code Playgroud)
Ric*_*ier 19
要获得正确的图像,您可以访问Android Asset Studio网站并选择Android Holo Colors Generator.这将创建您可能需要的所有资产,包括"三角形".它还会生成实现更改的XML文件.
以下是示例XML文件:
自定义颜色:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="apptheme_color">#33b5e5</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
自定义微调器样式:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="SpinnerAppTheme" parent="android:Widget.Spinner">
<item name="android:background">@drawable/apptheme_spinner_background_holo_light</item>
<item name="android:dropDownSelector">@drawable/apptheme_list_selector_holo_light</item>
</style>
<style name="SpinnerDropDownItemAppTheme" parent="android:Widget.DropDownItem.Spinner">
<item name="android:checkMark">@drawable/apptheme_btn_radio_holo_light</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
自定义应用主题:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="@style/_AppTheme"/>
<style name="_AppTheme" parent="Theme.AppCompat.Light">
<item name="android:spinnerStyle">@style/SpinnerAppTheme</item>
<item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItemAppTheme</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
如您所见,这些样式也引用了许多drawable.
以下是您要更改的"三角形"特有的:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="@drawable/apptheme_spinner_disabled_holo_light" />
<item android:state_pressed="true"
android:drawable="@drawable/apptheme_spinner_pressed_holo_light" />
<item android:state_pressed="false" android:state_focused="true"
android:drawable="@drawable/apptheme_spinner_focused_holo_light" />
<item android:drawable="@drawable/apptheme_spinner_default_holo_light" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我不认为这是完全列出每个文件的正确位置,因为您可以直接从引用的工具中获取它们.
注意:这是主题整个应用程序的方式,以便所有微调器都更新为自定义样式.
如果您正在寻找一种快速而肮脏的方式来改变一个微调器,那么请查看Ricky在他的评论中引用的那篇文章:
无论如何,我建议阅读它,因为它很好地解释了这个过程,并将帮助你理解我的其余答案.
jes*_*ems 12
我仍然是Android开发的新手,所以也许我会提一点建议,但是这里的答案似乎都与我使用的Spinner实现无关。
我在找什么
android:backgroundTint="@color/colorPrimary"
Run Code Online (Sandbox Code Playgroud)
这是整个<Spinner>标签:
<Spinner
android:id="@+id/coin_selector"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_margin="26dp"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:dropDownSelector="@color/colorAccent"
android:backgroundTint="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="89dp"/>
Run Code Online (Sandbox Code Playgroud)
就这样做吧。这将帮助您解决问题。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="spinner background image">
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="arrow image" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
要么
只是看看我为另一个查询给出的答案: 自定义微调器
这也是以编程方式解决该问题的另一种方法。(已通过API 19及更高版本进行了测试)。
使用ViewCompat此。
ViewCompat.setBackgroundTintList(spinner, ColorStateList.valueOf(your_color));
Run Code Online (Sandbox Code Playgroud)
用法spinner.setSupportBackgroundTintList抛出错误
如果您有 minSdkVersion 21,则非常简单:
<Spinner
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/triangleColor" />
Run Code Online (Sandbox Code Playgroud)
其中triangleColor 是您的颜色。
| 归档时间: |
|
| 查看次数: |
48476 次 |
| 最近记录: |