Android get screen size in XML

﹥>﹥吖頭↗ 提交于 2020-01-23 04:46:11

问题


Hey I have a scrollview from the top of the screen to a bit before the bottom of the screen because this is where my ad banner takes place. I want to have some pixels in between these elements ( between buttons and banner ) due the google policy. So far I did this with setting a value in dp. But as devices differ, this results in having a huge gap between the ad and the scrollview on some hi-res phones. This is why I'd like to set

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

(of course I didn't even try it in this way, as this is no code, but I think it sums up pretty well what I plan to do)

Thanks a lot for your answers!


回答1:


You can't query screen size in XML since it doesn't run at runtime, you can do this by using RelativeLayout and use alignments and margins.

in your case if Screen_height-banner represent a screen height and you want to position it at bottom and make a 5dp separator from end your XML would be

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

If you need to scale the scroll view instead of position it you xml would be

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



回答2:


You can user weight parameter. If you set weight of both layouts 0.5, this will share screen width equals for these layouts.

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



回答3:


Use a RelativeLayout, set a Margin of 5px and use layout_above



来源:https://stackoverflow.com/questions/19655225/android-get-screen-size-in-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!