布局无法在其他智能手机上正确显示

时间:2020-08-24 06:08:56

标签: android android-layout user-interface

我无法在智能手机中显示XML文件,如android studio所示。在某些智能手机中,广告下方的底部空间为空白,而在某些智能手机中,其底部会正确显示。

请更正我的XML代码,如下所示。预先感谢

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Version">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="470dp">
        <TextView
            android:id="@+id/tV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"

            android:scrollbars="vertical"
            android:text="@string/version"
            android:textColor="@color/black"
            android:textSize="19sp"
            app:fontFamily="sans-serif-condensed-light">
        </TextView>
    </ScrollView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/white">


        <Button
            android:id="@+id/zoomout"
            android:layout_width="38dp"
            android:layout_alignParentStart="true"
            android:layout_height="38dp"
            android:background="@drawable/zoom_out"
            android:layout_alignParentLeft="true">
        </Button>

        <Button
            android:id="@+id/trans_punjabi"
            android:layout_width="38dp"
            android:layout_toLeftOf="@id/zoonin"
            android:layout_height="38dp"
            android:background="@drawable/translation"
            ></Button>


        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="277dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginEnd="45dp"
            android:layout_marginLeft="13dp"
            android:layout_marginRight="45dp"
            android:layout_marginStart="13dp"
            android:layout_toEndOf="@+id/zoomout"
            android:layout_toLeftOf="@+id/zoonin"
            android:layout_toRightOf="@+id/zoomout"
            android:layout_toStartOf="@+id/zoonin" />

        <Button
            android:id="@+id/zoonin"
            android:layout_width="38dp"
            android:layout_height="38dp"
            android:layout_alignParentEnd="true"
            android:background="@drawable/zoom_in"
            android:layout_alignParentRight="true">
        </Button>

    </RelativeLayout>

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center"
            android:background="@color/white">

        <Button
            android:id="@+id/button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="13dp"
            android:layout_marginStart="13dp"
            android:layout_toEndOf="@+id/textView2"
            android:layout_toRightOf="@+id/textView2"
            android:background="@drawable/forward" />

        <Button
            android:id="@+id/button2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="-3dp"
            android:layout_marginStart="-3dp"
            android:layout_toEndOf="@+id/button"
            android:layout_toRightOf="@+id/button"
            android:background="@drawable/pauses" />

        <Button
            android:id="@+id/button3"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/button2"
            android:background="@drawable/plays" />

        <Button
            android:id="@+id/button4"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="3dp"
            android:layout_toRightOf="@+id/button3"
            android:background="@drawable/rewind" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:foregroundGravity="clip_horizontal"
            android:gravity="clip_horizontal|center"
            android:paddingLeft="14dp"
            android:text="0:0"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="7dp"
            android:layout_toRightOf="@id/button4"
            android:foregroundGravity="center_vertical"
            android:gravity="center_horizontal"
            android:paddingRight="14dp"
            android:text="5:23"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="24sp" />

    </RelativeLayout>
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
</LinearLayout>

我希望将textview(带有滚动条的长文本)显示在顶部,将广告显示在底部,并且底部不应有空白。

2 个答案:

答案 0 :(得分:0)

您的问题是您具有线性的垂直布局,其中包含一个固定大小的小部件(ScrollView)和少量wrap_content-这些值的总和(在测量之后)也是固定的大小/高度,因此在某些设备(尤其是大型设备)上,它会小于可用空间。尝试避免这样的固定大小声明:android:layout_height="470dp",尤其是对于ViewGroup s / View s的父母

使用RelativeLayout作为根布局,并在内部使用layout_abovelayout_alignParentBottomlayout_alignParentAbove attrs

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

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        .../>

    <RelativeLayout
        android:id="@+id/buttons_lower_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/adView"
        .../>

    <RelativeLayout
        android:id="@+id/buttons_upper_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/buttons_lower_row"
        .../>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_above="@id/buttons_upper_row"
        android:layout_alignParentTop="true"
        .../>
</RelativeLayout>
首先声明位于底部(View)的

AdView。然后用View声明下一个android:layout_above="@id/adView"(上面的广告),依此类推。最后ScrollView同时拥有layout_abovelayout_alignParentTop-将适合剩余的空白

PS。您可以想象layout_above=<id>this_layout_should_be_above_layout_with_id=<id>一样,还有layout_belowlayout_toRightOflayout_toLeftOf。所有这些都将View中的RelativeLayout边对边对齐

答案 1 :(得分:0)

我想为小部件设置固定大小,可以使用此library。它将根据屏幕尺寸缩放小部件。