小编Wir*_*ing的帖子

如何将自定义样式应用于SwitchCompat

我想将自定义样式应用于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.

android android-appcompat android-theme switchcompat

19
推荐指数
1
解决办法
2万
查看次数

如何将 Android DataStore 与多用户或文件一起使用

我想使用 DataStore 存储一些首选项。但问题是我的应用程序可以有多个用户,因此需要将这些首选项存储在单独的文件中。我得到了一个仅使用一个用户的工作示例,但我正在努力支持多个用户。

这是我的代码的示例:

class DataStorageRepository(private val context: Context, private val userRepository: UserRepository) {

    private object PreferencesKeys {
        val SETTING_ONE = intPreferencesKey("setting_one")
    }

    // retrieve datastore for currently logged in user. 
    private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = userRepository.currentRegistration().name)

    val userPreferencesFlow: Flow<UserPreferences> = context.dataStore.data.map { preferences ->
        val settingOne = preferences[PreferencesKeys.SETTING_ONE] ?: 0

        UserPreferences(settingOne)
    }

    suspend fun storeSettingOne(settingOne: Int) {
        context.dataStore.edit { preferences ->
            preferences[PreferencesKeys.SETTING_ONE] = settingOne
        }
    }

    data class UserPreferences(val lastUsedToAccountTab: Int)
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Koin并尝试卸载DataStorageRepository注销并在登录时重新创建它,但数据存储似乎一直保持活动状态,直到应用程序被终止并且出现以下崩溃: …

android koin android-jetpack coroutinescope android-jetpack-datastore

17
推荐指数
4
解决办法
1万
查看次数

Google Play控制台:如何将Android应用从Alpha推广到Beta

在我们公司内部,我们有人负责处理我们的Android应用上传和发布到Google Play控制台.他告诉我,之前有一个选项可以将应用程序从Alpha推广到Beta.如本视频所示,实际上有一个选项.

但是,我们目前看不到将Alpha应用程序移至Beta版的方法.只有一个选项可以将应用程序推广到生产环境.

是否仍然可以将Android应用从Alpha推广到Beta?或者我们应该再次将相同的应用上传到Beta.

android release-management apk app-distribution google-play-console

15
推荐指数
2
解决办法
2373
查看次数

在某些Android 4.X设备上未使用appcompat库应用主题

在将appcompat库更新到最新版本(v21)以应用材质设计之后,我的自定义主题不再在Android 4.0.X设备上使用,除非我在XML中明确声明它.

我在Android 2.3.X和5.0.0设备上测试了主题,它可以在这些设备上运行.但它不适用于我的HTC One S和Android 4.0.3.我需要做些什么来解决这个问题?

我的themes.xml

<style name="Theme.Custom" parent="@style/Theme.AppCompat.Light">
    <item name="android:editTextStyle">@style/Custom.Widget.EditText</item>
    <item name="android:buttonStyle">@style/Custom.Widget.Button</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我的styles.xml

<style name="Custom.Widget.EditText" parent="android:Widget.EditText">
    <item name="android:background">@drawable/edit_text_holo_light</item>
    <item name="android:textColor">@color/text_primary</item>
    <item name="android:textColorHint">@color/text_hint_color</item>
</style>

<style name="Custom.Widget.Button" parent="android:Widget.Button">
    <item name="android:background">@drawable/btn_default_holo_light</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:minWidth">64dip</item>
    <item name="android:textColor">@color/button_text_primary</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

    <!-- other elements -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp" >

        <EditText
            android:id="@+id/view_username_edit_username_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textNoSuggestions|textCapSentences"
            android:hint="@string/UsernameScreen_usernameHint"
            android:imeOptions="actionDone"
            android:singleLine="true"
            android:maxLength="@integer/username_max_length"
            android:textSize="@dimen/text_size_medium" />

        <Button
            android:id="@+id/view_username_save_Button"
            android:layout_width="match_parent" …
Run Code Online (Sandbox Code Playgroud)

android android-appcompat android-theme material-design

10
推荐指数
2
解决办法
3240
查看次数

Cardview角落背景不透明

我有一个CardView支持小部件的自定义实现,但是当我将它包含在我的布局文件中时,我似乎无法获得角落的背景透明.但是,如果我只是将CardView支持小部件放在我的布局文件中,它就会突然运行.如何让我的自定义组件的角落透明?

例

这是我自定义CardView实现的布局文件:

view_card.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/Custom.Widget.CardView">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/default_padding">

    <TextView
        android:id="@+id/view_mainText"
        style="@style/Custom.Widget.TextView.Header"
        android:textColor="@color/instruction_balloon_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/view_subText"
        android:textSize="@dimen/text_size_medium"
        android:textColor="@color/instruction_balloon_text"
        android:singleLine="false"
        android:text="Please remove white corners :-("
        android:textIsSelectable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

styles.xml

<style name="Custom.Widget.CardView" parent="CardView">
    <item name="cardBackgroundColor">@color/card_backgroundColor</item>
    <item name="cardCornerRadius">12dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这是我的布局文件,包括两个CardViews.第一个带有白色角落,第二个带有与view_card.xml基本相同的布局但没有白色角落(透明).

的example.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <some.private.namespace.CardView
        android:id="@+id/custom_card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/default_margin" />

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/view_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/default_margin"
        style="@style/Custom.Widget.CardView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="@dimen/default_padding">

            <TextView
                android:id="@+id/view_mainText"
                style="@style/Custom.Widget.TextView.Header"
                android:textColor="@color/instruction_balloon_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-layout android-support-library android-cardview

10
推荐指数
2
解决办法
7791
查看次数

Android:如何从自定义视图的超类中获取属性

我有一个A具有TextView 的自定义视图.我做了一个返回resourceIDTextView的方法.如果未定义文本,则默认情况下该方法将返回-1.我还有一个B从视图继承的自定义视图A.我的自定义视图的文本为"hello".当我调用方法来获取超类的属性时,我得到了-1.

在代码中还有一个例子,我可以如何检索值,但感觉有点hacky.

attrs.xml

<declare-styleable name="A">
    <attr name="mainText" format="reference" />
</declare-styleable>

<declare-styleable name="B" parent="A">
    <attr name="subText" format="reference" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

A级

protected static final int UNDEFINED = -1;

protected void init(Context context, AttributeSet attrs, int defStyle)
{
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.A, defStyle, 0);

     int mainTextId = getMainTextId(a);

     a.recycle();

     if (mainTextId != UNDEFINED)
     {
        setMainText(mainTextId);
     }
}

protected int getMainTextId(TypedArray a)
{
  return a.getResourceId(R.styleable.A_mainText, UNDEFINED);
}
Run Code Online (Sandbox Code Playgroud)

B级

protected void init(Context context, AttributeSet attrs, …
Run Code Online (Sandbox Code Playgroud)

inheritance android android-custom-view declare-styleable android-attributes

8
推荐指数
1
解决办法
2853
查看次数

Android CardView 应用主题

有没有办法将主题应用于 Cardview?我不想将样式应用于我正在创建的每个视图。

这是我现在的CardView

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/Custom.Widget.CardView">
    <-- I don't want to define the style every time -->
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

还有我的styles.xml

<style name="Custom.Widget.CardView" parent="CardView">
    <item name="cardBackgroundColor">@color/card_backgroundColor</item>
    <item name="cardCornerRadius">12dp</item>
    <item name="cardUseCompatPadding">true</item>
    <item name="contentPadding">4dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我想将样式添加到themes.xml以便将其应用于每个 Cardview,但我不知道如何。支持库中的视图甚至可能吗?

当我将以下内容添加到themes.xml 时,我收到一条警告:no resource found that matches the given name: attr 'Cardview'

<item name="Cardview">@style/Custom.Widget.CardView</item>
Run Code Online (Sandbox Code Playgroud)

android android-layout android-theme android-support-library android-cardview

6
推荐指数
1
解决办法
2075
查看次数

Android:检测设备是否可以使用指纹解锁

我有一个使用指纹认证的应用程序。作为一项额外的安全措施,我想检测指纹是否用于解锁设备。我尝试通过以下方式检索锁定模式:

long mode = android.provider.Settings.Secure.getLong(contentResolver, PASSWORD_TYPE_KEY,
            DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
Run Code Online (Sandbox Code Playgroud)

但是根据这篇文章检查是否启用了锁定,结果可能有所不同。

有没有办法确定使用指纹来解锁设备?

security android fingerprint device-policy-manager android-fingerprint-api

6
推荐指数
0
解决办法
736
查看次数

Android Espresso WebView webClick() 和 webKeys() 不会触发输入字段上的模糊事件

我正在尝试使用 Espresso 测试 WebView。WebView 包含一些输入字段和一个继续下一页的按钮。我可以在输入字段中输入文本。离开输入字段后,应该调用一个模糊事件来执行一些额外的格式化和验证。然而,模糊事件永远不会被调用,因此我无法继续下一页。

看起来webKeys()只是注入一些文本,而webClick()只是触发点击事件。这可能就是为什么焦点永远不会改变并且模糊事件永远不会被调用的原因。当我亲自按下输入字段时,事件确实被触发,并且我还看到了 Android 输入光标。当测试用例运行时,在输入字段中输入文本时我看不到光标。

有没有办法以编程方式触发模糊事件或使用 Espresso 将焦点设置到 WebView 中的元素?

这是我用来插入文本的代码。

public static void insertText(String label, String content)
{
    onWebView().withElement(findElement(Locator.XPATH, "//div[preceding::span[.='" + label + "']]/input")).perform(clearElement()).perform(webClick())
               .perform(webKeys(content));
}
Run Code Online (Sandbox Code Playgroud)

编辑:添加了相关依赖项。

implementation "androidx.test.espresso:espresso-idling-resource:3.2.0"
androidTestImplementation "androidx.test.espresso:espresso-core:3.2.0"
androidTestImplementation "androidx.test.espresso:espresso-intents:3.2.0"
androidTestImplementation "androidx.test.espresso:espresso-web:3.2.0"
androidTestImplementation "androidx.test.espresso:espresso-contrib:3.2.0"
Run Code Online (Sandbox Code Playgroud)

javascript android blur android-webview android-espresso

6
推荐指数
1
解决办法
1925
查看次数

特定维度和构建类型的 Gradle 依赖关系

我有一个具有多种风味维度的应用程序:

  • 版本(演示/免费/付费/vip)
  • 环境(测试/验收/生产)

以及常见的构建类型:debugrelease

这会产生构建变体的完整列表:

  • 演示测试版
  • 演示测试调试
  • 免费测试发布
  • 自由测试调试
  • 付费测试版
  • 付费测试调试
  • vip测试发布
  • vip测试调试
  • 其他环境依此类推。

然后我可以为这样的构建变体设置特定的依赖关系。

android {
    configurations {
        vipTestReleaseImplementation
        // dozens of other configurations.
    }
    dependencies {
        vipTestReleaseImplementation fileTree('my_test_library')
        // dozens of dependencies.
    }
}
Run Code Online (Sandbox Code Playgroud)

但这并不理想,因为我必须写下每一个组合,最终会得到数十种配置。

有没有办法定义 buildType 与特定维度的依赖关系?就我而言,环境维度?像这样:

android {
    configurations {
        // these configurations don't exist.
        testReleaseImplementation
        acceptanceReleaseImplementation
        productionReleaseImplementation
    }
    dependencies {
        debugImplementation fileTree('my_debug_library')

        // this would be nice but is not possible???
        testReleaseImplementation fileTree('my_test_library')
        acceptanceReleaseImplementation fileTree('my_test_library')
        productionReleaseImplementation fileTree('my_prod_library')
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用 avariantFilter …

configuration dependencies android gradle android-build-flavors

6
推荐指数
0
解决办法
440
查看次数