我的Switch按钮的拇指似乎变得歪斜(开启和关闭状态).在github上也有类似的问题,但那些是人们在API 4.0中使库支持Switch按钮 -
main包含开关按钮,并且有一个拇指和轨道可绘制的应用程序
这就是发生的事情:

这就是它的假设:

switch_track_on.png

main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="112dp"
android:thumb="@drawable/switch_thumb_selector"
android:track="@drawable/switch_track_selector"
android:textOn=""
android:textOff=""/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
switch_thumb_selector.xml
<selector>
<item android:drawable="@drawable/switch_thumb">
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
switch_track_selector.xml
<selector>
<item android:drawable="@drawable/switch_track_on" android:state_checked="true"/>
<item android:drawable="@drawable/switch_track_off" android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud) 我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" />
Run Code Online (Sandbox Code Playgroud)
在上面switch我toggle_button_color.xml用来switch分别打开和关闭时将拇指颜色改为绿色和红色.
<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>
Run Code Online (Sandbox Code Playgroud)
如果我将其添加switch到活动布局,然后将其完美地放在下面的图像中.

但如果我switch在Dialog使用时加上这个,m_dialog.setContentView(R.layout.mylayout);那么切换如下所示.请注意,这mylayout.xml是layout我添加的文件switch.

对于低于5.0的Android版本,棒棒糖switch正如我想的那样正常工作.请注意,由于某些原因我Theme.Holo.Light在我的应用程序中使用,所以我不能使用SwitchCompat.
我知道这里有一个类似的问题,点击Android 5.0时Switch崩溃了.
此处还会报告https://code.google.com/p/android-developer-preview/issues/detail?id=1704.我也试过上面链接中提到的工作为拇指和轨道添加可绘制的图像,但我不明白为什么同一个开关正在工作activity layout但没有Dialog.
有人可以帮我解决这个问题吗?
我已经创建了一个基本的自定义Switch,如下所述.
<Switch
android:id="@+id/availSwitch"
android:layout_width="wrap_content"
android:switchMinWidth="110dp"
android:layout_height="wrap_content"
android:track="@drawable/switch_track"
android:thumb="@drawable/thumb"/>
Run Code Online (Sandbox Code Playgroud)
这@drawable/thumb是一个简单的PNG,工作正常.
的@drawable/switch_track定义如下.@drawable/trackon并且@drawable/trackoff是PNG的.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/trackoff" />
<item android:state_checked="true" android:drawable="@drawable/trackon" />
<item android:drawable="@drawable/trackoff" />
</selector>
Run Code Online (Sandbox Code Playgroud)
这个开关在大多数情况下看起来和工作原理一样,但是有一些方法可以在用户拖动时拇指移过轨道来"动画"轨道吗?在已检查和未检查之间淡入淡出,或者最好在拇指后面"改变".
当前行为如下所示.
是否有任何解决方法可以帮助我在switch.setChecked(true);不触发的情况下以编程方式更改 Switch 小部件的状态OnCheckedChangedlistener()?
switch.setChecked(true);OnCheckedChangedlistener()自动触发,但我只需要在用户手动更改状态时触发它。所以我需要停止
switch.setChecked(true);触发OnCheckedChangedlistener()
我想使用Switch并更改其Track Color.所以在我看来没什么了不起的.
我的Switch布局:
<Switch
android:id="@+id/Switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:track="@color/gray"
android:textOn="@string/on"
android:textOff="@string/off"
android:text="@string/musiconoff" />
Run Code Online (Sandbox Code Playgroud)
我的颜色"灰色":
<color name="gray">#666666</color>
Run Code Online (Sandbox Code Playgroud)
我的问题是Switch显示为1像素的Switch.这只是一条小线.如果我删除"颜色"线,开关是正确的(当然没有灰色).
我的错在哪里?
我正在尝试制作这样的自定义开关:
具有这些特性:
这是我面临的两个问题,因为开关只显示所选一侧的文本,而且我似乎找不到可以指定两种不同颜色的地方?
我可以使用 android studio 中的常规开关来实现这一点,还是必须使用一些库?
谢谢你。
所以我有一个警报列表,并将其绑定到列表视图 lstAlarms。在我的自定义 ListView 布局中,我还有一个开关,我想根据警报的状态以编程方式设置它。我想在刚刚显示 ListView 后立即执行此操作。
请在下面查看我的代码。显示 ListView 的方法是 DisplayAlarmList()。我试图用来设置开关状态的方法是 InitSwitches(),它在 DisplayAlarmList() 内部被调用。DisplayAlarmList() 在 onCreate() 方法中调用。
public void DisplayAlarmList()
{
final String[] columns = {Database.getAlarmID(), Database.getAlarmTime(), Database.getAlarmName(), Database.getAlarmStatus(), Database.getAlarmRepeats()};
Cursor c = Database.selectAlarm(db, Database.getTableName(), columns, null, null, null, null, null);
int[] to = new int[]{
R.id.alarmID,
R.id.alarmTime,
R.id.alarmName,
R.id.alarmStatus,
R.id.alarmRepeats,
};
SimpleCursorAdapter ca = new SimpleCursorAdapter(this,
R.layout.alarm_info,
c,
columns,
to,
0);
lstAlarm.setAdapter(ca);
InitSwitches();
lstAlarm.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> listView, View view, int position, long id)
{
Alarm alarm …Run Code Online (Sandbox Code Playgroud) 我有一个自定义的 android 开关,其定义如下
<Switch
android:id="@+id/create_site_switch_ssl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:checked="true"
android:gravity="center_vertical"
android:thumb="@drawable/btn_gradient" />
Run Code Online (Sandbox Code Playgroud)
和拇指选择器项目像这样
<item android:state_enabled="true" android:state_focused="false" android:state_pressed="false">
<shape android:shape="rectangle">
<gradient
android:startColor="@color/Blue100"
android:endColor="@color/Blue50"
android:angle="90"/>
<padding android:left="@dimen/button_padding"
android:top="@dimen/button_padding"
android:right="@dimen/button_padding"
android:bottom="@dimen/button_padding" />
<corners android:radius="@dimen/button_corner"/>
<stroke android:color="@color/BlueBorder" android:width="5dp"/>
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
在 Android 版本 4.3、4.4.2 上,开关滑块在右侧和左侧像这样被剪切。
我尝试使用形状和开关的填充/边距/大小,到目前为止没有任何效果。
此外,使用适当的填充设置自定义轨道也不起作用。
在Android 5.0及更高版本上,通过设置解决了问题
android:paddingLeft
android:paddingRight
Run Code Online (Sandbox Code Playgroud)
为什么填充对于更高版本不起作用?我缺少什么?欢迎任何有关解决方案的提示!
我的问题是关于Android SwitchCompat.
我想要的风格:
但我的开关风格是:
我的拇指宽度有点不对劲。(请不要介意轨道大小)
这是我的 xml 代码:
<android.support.v7.widget.SwitchCompat
app:switchMinWidth="@dimen/_108px"
android:layout_width="@dimen/_108px"
android:layout_height="@dimen/_60px"
android:thumb="@drawable/switch_thumb_selector"
app:track="@drawable/switch_track_selector" />
Run Code Online (Sandbox Code Playgroud)
android:layout_width有助于设置轨道的宽度,并android:layout_height有助于设置拇指的高度。但我想设置拇指的宽度。如何设置拇指宽度?
我在我的应用程序中使用了switch小部件.当我在棒棒糖上运行app时,它的工作正常.但是当我在kitkat版本中运行时,它会崩溃.
xml设计:
<Switch
android:id="@+id/act_edit_device_swtch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchMinWidth="40sp"
android:thumb="@null"
android:track="@drawable/switchdb" />
Run Code Online (Sandbox Code Playgroud)
switchdb.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/onswitch" android:state_checked="true"/>
<item android:drawable="@drawable/offswitch" android:state_checked="false"/>
<item android:drawable="@drawable/offswitch"></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
下面是我的崩溃报告:
java.lang.NullPointerException
at android.widget.Switch.jumpDrawablesToCurrentState(Switch.java:825)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
at android.widget.FrameLayout.jumpDrawablesToCurrentState(FrameLayout.java:189)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:5500)
at android.widget.FrameLayout.jumpDrawablesToCurrentState(FrameLayout.java:189)
at android.view.View.onAttachedToWindow(View.java:12146)
at android.view.ViewGroup.onAttachedToWindow(ViewGroup.java:3499)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onAttachedToWindow(PhoneWindow.java:2659)
at android.view.View.dispatchAttachedToWindow(View.java:12620)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1236)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
Run Code Online (Sandbox Code Playgroud) android ×10
android-switch ×10
xml ×2
android-xml ×1
colors ×1
listview ×1
switchcompat ×1
widget ×1