Android以XML格式获取屏幕尺寸

时间:2013-10-29 10:13:24

标签: android layout

嘿我有一个从屏幕顶部到屏幕底部之前的滚动视图,因为这是我的广告横幅发生的地方。由于谷歌政策,我想在这些元素之间(按钮和横幅之间)有一些像素。到目前为止,我在dp中设置了一个值。但是,由于设备不同,这导致广告与某些高分辨率手机上的滚动视图之间存在巨大差距。这就是我想设置

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

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

非常感谢您的回答!

4 个答案:

答案 0 :(得分:4)

您无法在XML中查询屏幕大小,因为它不会在运行时运行,您可以使用RelativeLayout并使用alignmentsmargins来执行此操作。

在你的情况下,如果Screen_height-banner表示屏幕高度,并且你想将它放在底部并从最后创建一个5dp分隔符,那么

<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 >

如果您需要缩放滚动视图而不是定位它,那么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 >

答案 1 :(得分:4)

您可以使用用户权重参数。如果您将两个布局的权重设置为0.5,则这将共享这些布局的屏幕宽度等于。

 <LinearLayout
    android:id="@+id/alt_panel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/orta_panel" >

    <LinearLayout
        android:id="@+id/altsol_panel"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:gravity="center_horizontal"
        android:orientation="vertical" >


    </LinearLayout>

    <LinearLayout
        android:id="@+id/altsag_panel"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:gravity="center_horizontal"
        android:orientation="vertical" >


    </LinearLayout>
</LinearLayout>

答案 2 :(得分:2)

使用RelativeLayout,设置5px的保证金并使用layout_above

答案 3 :(得分:0)

如果以下代码对您不起作用,您可能使用的是较新版本的东西,您可以尝试使用 android:bottom="80dp" 而不是 android:layout_marginBottom="5dp"