小编Mar*_*ius的帖子

另一个视图限制ConstraintLayout中的宽度

ConstraintLayout只要在他的右边有另一个视图的空间,是否有可能让视图增长?

用例是除了彼此之外还有一个valueunitTextViews.该valueTextView的应该可以,只要有空间的成长unit.如果没有足够的空间,value应该切割.

ConstraintLayout

我用链子和其他一些东西尝试过但是无法完成它.在value不停止生长,然后unit再是不可见的.这是当前的代码:

<TextView
    android:id="@+id/value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:lines="1"
    app:layout_constraintBaseline_toBaselineOf="@+id/unit"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/unit"
    tools:text="12533939532" />

<TextView
    android:id="@+id/unit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintLeft_toRightOf="@id/value"
    app:layout_constraintRight_toRightOf="parent"
    tools:text="km" />
Run Code Online (Sandbox Code Playgroud)

android android-constraintlayout

26
推荐指数
1
解决办法
7164
查看次数

防止来自Facebook Android SDK的网络请求

即使尚未使用任何SDK,Android Facebook SDK也始终graph.facebook.com在调用时发出网络请求FacebookSdk.sdkInitialize(context).

因此,如果我们在初始化它,Application.onCreate()那么总会有至少一个网络请求.即使设置如下:

 <meta-data
    android:name="com.facebook.sdk.AutoLogAppEventsEnabled"
    android:value="false" />
Run Code Online (Sandbox Code Playgroud)

我们仔细研究了这一点,因为用户抱怨他没有使用Facebook登录(这就是我们首先拥有Facebook SDK的原因)并且仍有"用户数据"转移到Facebook.在GDPR和可疑用户时代,这是一种非常不利的行为!

我们现在正在做的是FacebookSdk.sdkInitialize(context)仅在用户想要使用Facebook登录时(在用户点击按钮时)进行呼叫.此外,我们删除了meta-dataandroid:name="com.facebook.sdk.ApplicationId"AndroidManifest.这可以防止初始网络请求,但随后出现以下崩溃CurrentAccessTokenExpirationBroadcastReceiver:

java.lang.RuntimeException: Unable to start receiver com.facebook.CurrentAccessTokenExpirationBroadcastReceiver: The SDK has not been initialized, make sure to call FacebookSdk.sdkInitialize() first.
at com.facebook.internal.Validate.sdkInitialized(Validate.java:143)
    at com.facebook.FacebookSdk.getApplicationContext(FacebookSdk.java:518)
    at com.facebook.AccessTokenManager.getInstance(AccessTokenManager.java:86)
    at com.facebook.CurrentAccessTokenExpirationBroadcastReceiver.onReceive(CurrentAccessTokenExpirationBroadcastReceiver.java:34)
Run Code Online (Sandbox Code Playgroud)

现在有几个问题:

  1. Facebook为什么还在开始时提出请求?如果他们想要验证身份验证令牌,他们可以在真正使用SDK时立即执行此操作...

  2. Facebook是否知道并容忍崩溃sdkInitialize()未被调用?因为我担心当它NullPointerException被删除时会有其他崩溃......

  3. 最重要的是:当没有使用其功能时,还有其他方法可以阻止来自Facebook SDK的网络请求吗?

android facebook facebook-android-sdk

21
推荐指数
1
解决办法
1112
查看次数

Retrofit2:如何正确检查response.body()的有效性?

从版本开始,即使在检查身体之前,retrofit2.retrofit:2.3.0我也会收到NullPointerException警告:response.body()null

图片

Method invocation 'getCar()' may produce 'java.lang.NullPointerException'
Run Code Online (Sandbox Code Playgroud)

在changelog中2.3.0是与null检查相关的条目:

Retrofit现在使用@Nullable来注释所有可能为空的值.[...]我们使用@ParametersAreNonnullByDefault,除非明确注释@Nullable,否则所有参数和返回类型都不会为null.

这是预期的行为吗?在我看来response.body()应该是不可变的,所以我在图1中的检查不应该显示警告.

这不是一个问题NullPointerExceptions- 它是关于如何正确处理Retrofit2的响应的方式.为了没有警告,我必须做这样的事情:

if(response != null) {
    CarResponseBody body = response.body();
    if (body != null && body.getCar() != null){
        CarResponse car = body.getCar();
    }
}
Run Code Online (Sandbox Code Playgroud)

很多代码只是检查是否有有效的响应...

java android retrofit retrofit2

10
推荐指数
1
解决办法
2485
查看次数

使用 Android Jetpack Navigation 预加载片段以实现平滑过渡

是否可以Fragments使用Android Jetpack 中的导航架构组件预先加载某些内容以获得平滑过渡?

如果没有预加载,我将永远无法获得第一次navigate()通话的平滑过渡(即使在高端设备上)。我目前使用的app:enterAnimapp:exitAnim等标签我nav_graph.xml

我正在尝试像在 Telegram 应用程序中那样完成平滑的过渡(我认为他们正在预加载所有内容并且只是在Fragments类似中制作动画Views)。

是否有一些隐藏的 XML 属性app:preload="true"

我不仅要求对我的问题提供全面的答案 - 也欢迎小优化!

我走了多远:

anim_nav_enter.xml

<set
    android:duration="200"
    android:shareInterpolator="true">
    <!-- Fade in from the right on enter -->
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
    <translate
        android:fromXDelta="10%"
        android:toXDelta="0%" />
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
    </set>
Run Code Online (Sandbox Code Playgroud)

anim_nav_exit.xml

<set
    android:duration="300"
    android:shareInterpolator="true">
    <!-- Fade out on exit -->
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.0"
        android:toYScale="1.0" /> …
Run Code Online (Sandbox Code Playgroud)

android android-transitions android-jetpack android-architecture-navigation

8
推荐指数
0
解决办法
650
查看次数

检测位置的"蓝牙扫描"是否已打开

从Android M开始,如果您在位置设置中启用了蓝牙扫描选项,即使全局位置已关闭,也可以在后台扫描蓝牙设备(请参见屏幕截图).

要扫描BLE设备,必须满足以下条件:

  • COARSE_LOCATIONFINE_LOCATION许可.

以下之一:

  • 启用全局位置选择器.
  • 启用蓝牙扫描选项(见屏幕截图).

我可以检查授予权限和位置选择器的状态就好了.我无法做到的是弄清楚如何检查Bluetooth Scanning选项的状态?

任何见解都非常感谢!

棉花糖蓝牙扫描选项

android bluetooth android-location bluetooth-lowenergy android-bluetooth

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

如何在 Android 项目中使用 Kotlin 工作表

我想在我的 Android 项目中使用Kotlin 工作表,以便添加如下代码草稿:

草稿.ws.kts

package com.example.app

val a = 1 + 1
a
Run Code Online (Sandbox Code Playgroud)

工作表本身正在工作:

val a: Int
2
Run Code Online (Sandbox Code Playgroud)

但是构建我的 Android 应用程序失败并显示以下输出:

> Task :appicals:compileDebugKotlin FAILED
e: org.jetbrains.kotlin.util.KotlinFrontEndException: Front-end Internal error: Failed to analyze declaration Draft_ws
File being compiled: (1,43) in /Users/me/secretproject/app/src/main/java/com/example/app/draft.ws.kts
The root cause org.jetbrains.kotlin.resolve.lazy.NoDescriptorForDeclarationException was thrown at: org.jetbrains.kotlin.resolve.lazy.BasicAbsentDescriptorHandler.diagnoseDescriptorNotFound(AbsentDescriptorHandler.kt:18)
    at org.jetbrains.kotlin.resolve.ExceptionWrappingKtVisitorVoid.visitDeclaration(ExceptionWrappingKtVisitorVoid.kt:43)
    at org.jetbrains.kotlin.psi.KtVisitorVoid.visitDeclaration(KtVisitorVoid.java:453)
    at org.jetbrains.kotlin.psi.KtVisitorVoid.visitDeclaration(KtVisitorVoid.java:21)
    at org.jetbrains.kotlin.psi.KtVisitor.visitScript(KtVisitor.java:78)
    at org.jetbrains.kotlin.psi.KtVisitorVoid.visitScript(KtVisitorVoid.java:73)
    at org.jetbrains.kotlin.psi.KtVisitorVoid.visitScript(KtVisitorVoid.java:519)
    at org.jetbrains.kotlin.psi.KtVisitorVoid.visitScript(KtVisitorVoid.java:21)
    at org.jetbrains.kotlin.psi.KtScript.accept(KtScript.java:69)
    at org.jetbrains.kotlin.psi.KtElementImplStub.accept(KtElementImplStub.java:59)
    at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer$analyzeDeclarations$1.registerDeclarations(LazyTopDownAnalyzer.kt:78)
    at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer$analyzeDeclarations$1.visitKtFile(LazyTopDownAnalyzer.kt:96)
    at org.jetbrains.kotlin.psi.KtVisitorVoid.visitKtFile(KtVisitorVoid.java:513)
    at org.jetbrains.kotlin.psi.KtVisitorVoid.visitKtFile(KtVisitorVoid.java:21)
    at org.jetbrains.kotlin.psi.KtFile.accept(KtFile.kt:242) …
Run Code Online (Sandbox Code Playgroud)

android kotlin kotlin-script

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

LibGDX - 具有固定比例的缩放图像

我的LibGDX游戏有一个带有徽标和一些按钮的开始屏幕.我无法弄清楚如何以固定的比例缩放徽标以免扭曲它.

那是代码:

public StartScreen(int lastScore, InputListener inputListener) {
    super();
    this.listener = inputListener;
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);

    Table table = new Table();
    table.setFillParent(true);
    table.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("background.png")))));

    Image imageLogo = new Image();
    imageLogo.setDrawable(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("title.png")))));

    // Here's the problem, the image is not scaled correctly...
    table.add(imageLogo).center().colspan(2);
    table.row();

    // Some Buttons
    table.add(button1).colspan(2).padBottom(30);
    table.row();
    table.add(button2).padBottom(30);
    table.add(button3).padBottom(30);
    table.row();
    table.add(button4).colspan(2);

    stage.addActor(table);

    //table.debug();
}
Run Code Online (Sandbox Code Playgroud)

layout android libgdx

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

抑制某些类别中的 PMD 违规

我有ViewStatePOJO 类,它们的构造函数带有许多参数。问题是 PMDExcessiveParameterList对他们施加了违规行为。

现在我试图抑制所有以ViewState.java(例如 in DashboardViewState.java)结尾的类的这种违规行为。我已将其添加到我的rules-pmd.xml

<rule ref="category/java/design.xml/ExcessiveParameterList">
    <properties>
        <!--Ignore ExcessiveParameterList on ViewState classes -->
        <property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration['*ViewState.java']"/>
    </properties>
</rule>
Run Code Online (Sandbox Code Playgroud)

问题是,这将压制所有针对ExcessiveParameterList哪个阶级的违规行为。我究竟做错了什么?

java static-analysis pmd suppress-warnings static-code-analysis

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