安卓开发--问题记录

如何关闭导航栏

问题:如图所示,我们想导航栏与页面融为一体:

办法:在 com/example/ecgphone/MainActivity.java中使用代码:

//         将顶部状态栏设置为透明,关闭顶部状态栏的高度
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.activity_main);

效果:

可以看到顶部状态栏成功融入页面。

如何关闭摄像头黑边

问题:如图所示,在页面左边因为存在摄像头刘海屏,导致摄像头区域全为黑边

办法:在 values/themes.xml中使用代码:

<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>

效果:

成功关闭左边黑边!!!
参考链接:Android刘海屏横屏时有黑边不全屏的解决方案

http 明文引用问题

现象:
Log打印报错:

Network request failed
    java.net.UnknownServiceException: CLEARTEXT communication to 40c53f71.r27.cpolar.top not permitted by network security policy
    	at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:188)
    	at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
    	at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
    	at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
    	at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
    	at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
    	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    	at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
    	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    	at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
    	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
    	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
    	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
    	at java.lang.Thread.run(Thread.java:1012)

问题分析:从Android 9(API level 28)开始,默认情况下不允许明文HTTP通信。

办法一:使用HTTPS
如果服务器支持HTTPS,使用HTTPS URL代替HTTP URL。

http://...            ------>                  https://...

办法二:允许明文HTTP通信
修改应用的网络安全配置,允许明文HTTP通信。在res/xml目录创建network_security_config.xml的文件,并写入代码:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">这里填http://(这部分)</domain>
    </domain-config>
</network-security-config>

然后,AndroidManifest.xml中引用这个配置文件:

<application
    ...
    android:networkSecurityConfig="@xml/network_security_config"
    ... >
    ...
</application>

在Fragment活动文件中使用toast

方法:

getActivity().runOnUiThread(() ->
    Toast.makeText(getActivity(), "Response: " + responseData, Toast.LENGTH_SHORT).show()
);

解释:
1 getActivity().runOnUiThread(() -> { … }):

  • getActivity() 获取当前 fragment 所附加的 Activity
  • runOnUiThread() 方法用于确保指定的代码在主线程(也称为 UI 线程)上运行。Android 要求所有与 UI 相关的操作必须在主线程上执行。
  • () -> { … } 是一个 lambda 表达式,用于定义在主线程上执行的代码。

2 为什么需要在主线程上运行?
在 Android 中,只有主线程可以更新 UI 元素。如果尝试在工作线程(如网络请求回调线程)中直接更新 UI,会导致应用崩溃。因此,需要使用 runOnUiThread() 将 UI 更新操作切换到主线程上执行。

更改APP头像,且适应各种大小

在 Android Studio 中,更换应用程序的图标(软件头像)涉及修改应用的资源文件,以下是步骤:

  1. 准备图标图像文件:图标应该是 PNG 格式的图片。
  2. 使用 Image Asset 工具:右键点击 app 文件夹,选择 New > Image Asset
  3. 配置图标:选择图标类型:Launcher Icons (Adaptive and Legacy) 或 Legacy Only,在 Path 部分,点击 Clip Art 或 Image,并选择你准备好的图标文件,配置其他选项,如前景层和背景层(如果适用)。
  4. 生成图标:选择 Next,点击 Finish,Android Studio 将会自动生成并放置适当尺寸的图标到 res/mipmap 文件夹中。
  5. 更新 AndroidManifest.xml
  6. Build > Clean Project,Build > Rebuild Project
  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值