gpt4 book ai didi

Android 在 XML 中获取屏幕尺寸

转载 作者:可可西里 更新时间:2023-11-01 19:06:25 27 4
gpt4 key购买 nike

嘿,我有一个从屏幕顶部到屏幕底部之前一点的 ScrollView ,因为这是我的广告横幅出现的地方。由于谷歌政策,我想在这些元素之间(按钮和横幅之间)有一些像素。到目前为止,我是通过在 dp 中设置一个值来做到这一点的。但由于设备不同,这会导致某些高分辨率手机上的广告和 ScrollView 之间存在巨大差距。这就是为什么我想设置

<ScrollView 
android:layout_height="(Screen_height-banner)-5px"

(当然我什至没有尝试过这种方式,因为这不是代码,但我认为它很好地总结了我打算做的事情)

非常感谢您的回答!

最佳答案

您不能在 XML 中查询屏幕尺寸,因为它不会在运行时运行,您可以通过使用 RelativeLayout 并使用 alignmentsmargins 来实现

在您的情况下,如果 Screen_height-banner 代表屏幕高度并且您希望将其放置在底部并从末尾制作一个 5dp 分隔符,您的 XML 将是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="5dp">

</RelativeLayout >

如果您需要缩放 ScrollView 而不是定位它,您可以使用 xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding_bottom="5dp">

<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent">

</RelativeLayout >

关于Android 在 XML 中获取屏幕尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655225/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com