[英]Switch control is not working on Dialog in Android version 5.0
I have used below switch
in my application. 我在应用程序中使用了以下switch
。
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text=""
android:thumb="@drawable/toggle_button_color"
android:textOff="@string/text_estimate"
android:textOn="@string/text_accurate"
android:textColor="@color/white" />
In above switch
I am using toggle_button_color.xml
to change the thumb color to green and red when switch
is on and off respectively. 在上面的switch
,当switch
分别打开和关闭时,我使用toggle_button_color.xml
将拇指颜色更改为绿色和红色。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@color/red" />
<item android:state_checked="true" android:drawable="@color/green" />
</selector>
If I add this switch
to an activity layout and then its wokring perfectly as below image. 如果我将此switch
添加到活动布局,然后将其完全唤醒,如下图所示。
But if I add this switch
on Dialog
using m_dialog.setContentView(R.layout.mylayout);
但是如果我使用m_dialog.setContentView(R.layout.mylayout);
在Dialog
上添加此switch
m_dialog.setContentView(R.layout.mylayout);
then switch looks like below. 然后切换如下图所示。 Note that here mylayout.xml
is a layout
file in which I have added switch
. 注意,这里mylayout.xml
是一个layout
文件,其中添加了switch
。
For android version below 5.0 lollipop switch
is working fine as I want. 对于低于5.0的Android版本,棒棒糖switch
可以按我的要求正常工作。 Note that for some reasons I am using Theme.Holo.Light
in my application so I can't use SwitchCompat
. 请注意,由于某些原因,我在应用程序中使用Theme.Holo.Light
,因此无法使用SwitchCompat
。
I know that a similar question has been asked here Switch crashes when clicked on Android 5.0 . 我知道在这里有人问过类似的问题, 当您在Android 5.0上单击鼠标时,开关会崩溃 。
And also it is reported here https://code.google.com/p/android-developer-preview/issues/detail?id=1704 . 并在这里https://code.google.com/p/android-developer-preview/issues/detail?id=1704进行报告。 I have also tried the work around which is mentioned in above link to add drawable image for thumb and track, but I don't understand why same switch is working on activity layout
but not on Dialog
. 我还尝试了上述链接中提到的解决方法,以添加用于拇指和轨迹的可绘制图像,但是我不明白为什么相同的开关在activity layout
上起作用而在Dialog
上却没有起作用。
Can anybody please help me out with this? 有人可以帮我这个忙吗?
Thank you all for your response, but I solved it by my own. 谢谢大家的回应,但我自己解决了。 Earlier I was implementing the dialog using the Dialog
class, which was causing the problems. 之前,我是使用Dialog
类实现对话框的,这引起了问题。
Dialog mDialog= new Dialog(getActivity(),android.R.style.Theme_Dialog);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.mylayout);
I have even tried changing the themes
but it didn't help. 我什至尝试更改themes
但没有帮助。
Then I tried using DialogFragment
, which solved the issue. 然后,我尝试使用DialogFragment
解决了问题。
public class MyDialog extends DialogFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
View v = inflater.inflate(R.layout.mylayout, container, false);
return v;
}
}
And from my Activity
class I invoke this Dialog
as below. 然后从我的Activity
类中调用以下Dialog
。
MyDialog mDialog = new MyDialog();
mDialog .show(getFragmentManager(), "Hello");
I can't be sure without seeing your Dialog
instantiation code (please add that if you can), but it sounds like there's a discrepancy between the Theme
being used for your Activity
and the theme being used for your Dialog
. 我无法确定是否看不到您的Dialog
实例化代码(如果可以的话,请添加该代码),但是听起来好像Activity
所用的Theme
与Dialog
所用的主题之间存在差异。 You may want to experiment with explicitly specifying the Dialog
's Theme
using the public Dialog (Context context, int theme)
constructor . 您可能想尝试使用public Dialog (Context context, int theme)
构造函数显式指定Dialog
的Theme
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.