我的应用程序的基本主题是:
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
<item name="android:actionBarTabBarStyle">@style/MyActionBarTabBar</item>
<item name="android:actionBarTabStyle">@style/MyActionBarTabView</item>
<item name="android:popupMenuStyle">@style/MyMenu</item>
<!--<item name="android:actionBarStyle">@style/window_action_bar_style</item>-->
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:windowContentOverlay">@null</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我想将这个主题应用于一项活动.
<style name="AppTheme" parent="Theme.AppCompat.Light">
</style>
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我无法访问该活动中的SwitchCompat小部件:
<android.support.v7.widget.SwitchCompat/>
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?任何建议或解决方案将是最受欢迎的.当我尝试覆盖特定活动中的基本主题并尝试访问SwitchCompat小部件时,会发生此问题.我的项目已经非常大,我可以更改整个应用程序的基本主题,因为我将不得不对ActionBars和其他视图进行更改.怎么解决这个问题?
是否可以使用android Data Binding编写switch case?
假设我有3个条件,比如
value == 1 then print A
value == 2 then print B
value == 3 then print C
Run Code Online (Sandbox Code Playgroud)
有没有办法通过使用数据绑定在xml中执行此操作?
我知道我们可以实现条件语句
android:visibility="@{age < 13 ? View.GONE : View.VISIBLE}"
Run Code Online (Sandbox Code Playgroud)
但在这里我正在寻找切换案例陈述.
我正在尝试将Android Switch作为两个选项之间的选择,所以我想做到这样,当开关"打开"时它与"关闭"时的开关颜色相同.我该怎么做呢?
我正在尝试在"设置"页面中使用SwitchPreference,但我无法让侦听器工作.我正在使用自定义小部件布局来设置切换样式,因为我找不到如何使用主题来实现这一点.在我的settings.xml中,我有以下内容:
<SwitchPreference
android:key="uitestmode"
android:summary="Enable to display test data in fragments"
android:title="UI Test Mode"
android:widgetLayout="@layout/widget_custom_switch_on_off" >
</SwitchPreference>
Run Code Online (Sandbox Code Playgroud)
我的SettingsFragment看起来像:
SwitchPreference uiTestModePref = (SwitchPreference) findPreference("uitestmode");
uiTestModePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(preference.getKey(), newValue.toString());
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
和我的交换机的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_switch_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textIsSelectable="false"
android:textSize="18sp"
android:textOn="On"
android:textOff="Off"
android:textStyle="bold"
android:thumb="@drawable/carcast_switch_inner_holo_dark"
android:track="@drawable/carcast_switch_track_holo_dark" />
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我单击开关时,不会调用侦听器.有谁知道为什么以及如何解决它?
在我的 android 应用程序中,我有一个开关小部件,我设置了一个像这样的 oncheckchange 侦听器
main.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if (no_internet()) {
// cancel click...
return;
}
if (image.getApproved()) {
if (arg1) {
for(FishImage image : imagestoshow) {
image.setIsMain(false);
}
}
image.setIsMain(arg1);
}
}
});
Run Code Online (Sandbox Code Playgroud)
基本上,如果有互联网,我会做一些事情,但如果没有,那么我想取消对交换机的更改。现在,它将改变开关的状态。如果我尝试更改代码中的状态,由于此代码发生在 android 更改状态之前,因此不会产生任何效果。
我需要一种取消开关的方法,所以如果我这样做了,就好像我从未点击过它一样。
我使用开关创建了一个布局文件。现在我想改变它的选择器的形状。我还想删除这个开关上的文本。
目前它具有矩形形状。我想把它改成圆形。
这是我的布局文件,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_remind_me"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.pvt.RemindMeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/remind_me_head_txt"
android:layout_marginTop="5dp"
android:textSize="20sp"
android:text="Example one"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_weight="1">
<Switch
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:showText="false"
android:id="@+id/remind_me_switch"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="@+id/remind_me_list_img"
android:src="@drawable/list_menu" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/remind_me_title_txt"
android:text="Sample Head"
android:textSize="12sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<TextView
android:layout_width="match_parent" …Run Code Online (Sandbox Code Playgroud)