小编sok*_*ive的帖子

如何在 Android 项目中使用谷歌翻译 api v3 将 googleapis/java-translate 库转换为自定义 api rest 请求

由于googleapis/java-translate库,在为我的应用程序构建版本时看到冲突之前,我使用了这些行并且它工作得很好:

    val translate: Translate = TranslateOptions.newBuilder().setApiKey(API_KEY).build().service
    val translations = translate.translate(
         textsToTranslate,
         Translate.TranslateOption.sourceLanguage(sourceLanguage),
         Translate.TranslateOption.targetLanguage(targetLanguage)
    )
Run Code Online (Sandbox Code Playgroud)

然后,在构建 release 时,我不得不在 app/build.gradle 中添加一些额外的代码以使其工作。但是,我看到我的应用程序从 10Mo 增长到 15Mo。只是翻译一些文本就很贵..

所以我决定使用最新的 Google Translate Api v3链接自己执行这些翻译

执行一个简单的 api 休息请求,如下所示:

    val jsonObjectResponse = JSONObject(translate(sourceLanguageCode = sourceLanguage, targetLanguageCode = targetLanguage, textsToTranslate).awaitString())
    val translations = jsonObjectResponse.getJSONArray("translations").toArray { getJSONObject(it) }.map { it.getString("translatedText") }
Run Code Online (Sandbox Code Playgroud)

其中“翻译”功能是:

    private fun translate(sourceLanguageCode: String, targetLanguageCode: String, textsToTranslate: List<String>): Request =
        Fuel.post(path = "https://translation.googleapis.com/v3/projects/$PROJECT_ID:translateText?key=$API_KEY")
            .header(Headers.ACCEPT to "application/json")
            .jsonBody(
                JSONObject().apply {
                    put("contents", JSONArray().apply {
                        textsToTranslate.forEach { text …
Run Code Online (Sandbox Code Playgroud)

api rest android kotlin google-translation-api

5
推荐指数
1
解决办法
221
查看次数

如何在xml(值文件夹中的dimens.xml)中设置宽高比以根据设备设置不同的宽高比?

也许是一个愚蠢的问题,但在android中如何在xml(值文件夹中的dimens.xml)中设置宽高比以根据设备设置不同的宽高比

我试过 :

布局.xml

app:layout_constraintDimensionRatio="@dimen/common_image_aspect_ratio"
Run Code Online (Sandbox Code Playgroud)

值/dimens.xml

<dimen name="common_image_aspect_ratio">3:2</dimen>
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用。

任何帮助将不胜感激,谢谢:)

android aspect-ratio android-constraintlayout

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