我的应用程序中有一些不同颜色的开关控件,并且我使用多个自定义可绘制选择器来更改颜色.
随着AppCompat v21库的发布,引入了一个新的android.support.v7.widget.SwitchCompat控件.
是否可以在没有客户可绘制选择器的情况下以编程方式更改SwitchCompat的颜色,但是使用XML或代码?
我在recyclerview中有一个开关,在从DB检索数据后,数据显示在recyclerview中.打开recyclerview时,我读DB,如果DB中的字段是"Y",我启用开关,否则我禁用开关.现在问题是它还调用了onCheckedchanged侦听器,我希望只有当用户手动设置开关时才调用onCheckedChanged.
在打开时,执行下面的recyclerview:
holder.enabledisable.setChecked(messengerRecord.get_is_valid().equalsIgnoreCase("Y"));
Run Code Online (Sandbox Code Playgroud)
ViewHolder类:
public class viewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener{
public SwitchCompat enabledisable;
public viewHolder(View v) {
enabledisable = (SwitchCompat) v.findViewById(R.id.enabledisable);
enabledisable.setOnCheckedChangeListener(this);
...................................
...................................
Run Code Online (Sandbox Code Playgroud)
刚刚打开recyclerView时调用的OncheckedChanged方法:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.v("ranjith","called oncheckedchanged");
MessengerRecord rec;
rec = dbHelper.getRecord(descview.getText().toString());
switch (buttonView.getId()) {
case R.id.enabledisable:
if (isChecked) {
rec.set_is_valid("Y");
dbHelper.updateRecord(rec);
}
}
Run Code Online (Sandbox Code Playgroud)
在布局文件中:
<android.support.v7.widget.SwitchCompat
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:id="@+id/enabledisable"
android:layout_alignRight="@+id/textview_to"
android:layout_alignEnd="@+id/textview_to"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
Run Code Online (Sandbox Code Playgroud) android oncheckedchanged android-switch switchcompat android-recyclerview
我尝试使用以下链接更改SwitchCompat的颜色:
注意我的开关中的低对比度:

但在更改所有相关颜色值后,SwitchCompat的轨道(亮灰色)保持不变.除了颜色,我不想改变外观.拇指是粉红色的,我希望轨道有一些对比.我错过了在styles.xml中定义一个值吗?
我尝试了这些值(随机非白色):
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/first</item>
<item name="colorPrimaryDark">@color/second</item>
<item name="colorAccent">@color/third</item>
...
<item name="colorControlActivated">@color/first</item>
<item name="colorControlHighlight">@color/first</item>
<item name="colorControlNormal">@color/second</item>
<item name="colorSwitchThumbNormal">@color/second</item>
<item name="colorButtonNormal">@color/second</item>
...>
Run Code Online (Sandbox Code Playgroud) 我想将自定义样式应用于SwitchCompat.更改可打开和关闭状态的drawable和文本.我怎样才能做到这一点?我找不到任何关于如何做到这一点的例子.我在styles.xml中尝试了以下内容,但显然我没有使用正确的父:
<style name="Switch" parent="android:Widget.AppCompat.Widget.CompoundButton.SwitchCompat">
<item name="android:textOn">@string/common_yes</item>
<item name="android:textOff">@string/common_no</item>
<item name="android:thumb">@drawable/btn_switch_selector</item>
<item name="android:track">@drawable/btn_switch_bg_selector</item>
</style>
Run Code Online (Sandbox Code Playgroud)
编辑
我设法在代码中更改drawables.
switchView.setThumbResource(R.drawable.btn_switch_selector);
switchView.setTrackResource(R.drawable.btn_switch_bg_selector);
Run Code Online (Sandbox Code Playgroud)
但我还没有找到改变开关文本的方法.以下代码段似乎不起作用.也许我需要设置更多的文本属性?
switchView.setTextOn(context.getString(R.string.common_yes));
switchView.setTextOff(context.getString(R.string.common_no));
Run Code Online (Sandbox Code Playgroud)
根据SwitchCompat源代码,应支持开/关文本:https: //android.googlesource.com/platform/frameworks/support/+/421d8baa4a524e1384bcf033360bccaf8d55081d/v7/appcompat/src/android/support/v7/widget/ SwitchCompat.java
{@link #setText(CharSequence)text}属性控制切换标签中显示的文本,而{@link #setTextOff(CharSequence)off}和{@link #setTextOn(CharSequence)on}文本控制文本在拇指上.
编辑2
终于找到了代码解决方案.显然setShowText()需要设置true为文本显示在开关上.
switchView.setTextOn(context.getString(R.string.common_yes));
switchView.setTextOff(context.getString(R.string.common_no));
switchView.setShowText(true);
switchView.setThumbResource(R.drawable.btn_switch_selector);
switchView.setTrackResource(R.drawable.btn_switch_bg_selector);
Run Code Online (Sandbox Code Playgroud)
和xml解决方案
<android.support.v7.widget.SwitchCompat
android:id="@+id/view_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/btn_switch_selector"
app:track="@drawable/btn_switch_bg_selector"
android:textOn="@string/common_yes"
android:textOff="@string/common_no"
app:showText="true" />
Run Code Online (Sandbox Code Playgroud)
我还是想知道是否有办法把它放进去styles.xml.
似乎AppCompat v21提供的SwitchCompat不提供SwitchCompatPerefrence.
我知道我可以使用SwitchPreference但它在视觉上并不相同.在Android 4.x上; 当我在活动界面上使用v21的SwitchCompact时,它看起来像是材质切换按钮,但是,因为没有SwitchCompactPreference,我必须在我的pereference视图中使用SwitchPreference,显然它有Android 4.0外观.
它看起来像AppCompact v21半完成.
我错过了什么吗?
我试图模仿我的应用程序中的新材料设计开关,并且switchcompat工作得很好.唯一的问题是触摸开关周围会出现一个灰色选择框,真正消除干净的外观.这是我可以删除的东西吗?

我已经阅读了一些关于颜色的线程,但是所有这些都必须通过style.xml进行设置.
现在我用它来确定颜色.
Run Code Online (Sandbox Code Playgroud)<style name="Color1SwitchStyle"> <item name="colorControlActivated">#0e8488</item> </style>'
是否可以在不使用XML的情况下更改SwitchCompat/Checkbox的颜色,例如使用代码?
尝试将SwitchCompat设置为操作栏时出错.
这是我的abswitch.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/abswitch"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v7.widget.SwitchCompat
android:id="@+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是我的profile_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/myswitch"
android:title=""
app:showAsAction="always"
android:actionLayout="@layout/abswitch"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
在这里onCreateOptionsMenu来自片段
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.profile_menu, menu);
MenuItem item = menu.findItem(R.id.myswitch);
item.setActionView(R.layout.abswitch);
SwitchCompat switchCompat = (SwitchCompat) item.getActionView();
super.onCreateOptionsMenu(menu, inflater);
}
Run Code Online (Sandbox Code Playgroud)
这是每次我尝试启动我的应用程序时得到的错误
Process: ru.dennes544.vkapp, PID: 15974
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.SwitchCompat
at ru.dennes544.vkapp.Profile.onCreateOptionsMenu(Profile.java:216)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1868)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1989)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:276)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
at …Run Code Online (Sandbox Code Playgroud) android toolbar android-appcompat android-fragments switchcompat
我正在使用android.support.v7.widget.SwitchCompat,我遇到了以下问题
我的代码
Styles.xml
注意我试过没有父和Theme.AppCompat.Light.NoActionBar
<style name="ToggleSwitchStyle" parent="Theme.AppCompat">
<item name="colorControlActivated">@color/emerald</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我的SwitchCompat在XML布局中定义
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:padding="5dp"
android:textOff="@string/no"
android:textOn="@string/yes"
app:showText="true"
android:switchPadding="5dp"
app:switchPadding="10dp"
app:theme="@style/ToggleSwitchStyle"
android:theme="@style/ToggleSwitchStyle"
android:textAllCaps="true"
app:thumbTextPadding="5dp"
>
Run Code Online (Sandbox Code Playgroud)
所以在上面的文字中,AllCaps不会使拇指上的文字全部大写.
切换填充无效
使用Res-Auto或Android命名空间的主题对活动颜色没有影响.
但是,我可以通过更改材质主题的颜色重音来更改活动颜色
<!-- Application theme. -->
<style name="MaterialDesign" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/yellow</item>
</style>
Run Code Online (Sandbox Code Playgroud) 我有以下代码将SwitchCompat与RecyclerView一起使用.
SwitchCompat所在的列表项布局:fragment_notification_settings_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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="65dp"
android:background="@android:color/white">
<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="15dp"
android:src="@drawable/settings_about"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_centerVertical="true"
android:tint="@color/Black"
tools:ignore="ContentDescription"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/icon"
android:layout_toStartOf="@+id/on_off_switch"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingStart="15dp"
android:paddingEnd="0dp"
android:text="Kaza Takibi"
android:textSize="18sp"
android:textColor="@color/Black"
tools:ignore="HardcodedText"/>
<TextView
android:id="@+id/item_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingStart="15dp"
android:paddingEnd="0dp"
android:text="Ezan vakitlerinde namaz k?lma uyar?s?"
android:textSize="16sp"
android:textColor="@color/DarkGray"
tools:ignore="HardcodedText"/>
</LinearLayout>
<android.support.v7.widget.SwitchCompat
android:id="@+id/on_off_switch"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
片段类:
public class NotificationSettingsFragment extends Fragment{
@Bind(R.id.close_button)
ImageView closeButtonImageView;
@Bind(R.id.recycler_view)
RecyclerView recyclerView;
private MainActivity activity;
private …Run Code Online (Sandbox Code Playgroud) android nullpointerexception switchcompat android-recyclerview