繁体   English   中英

如何在任何屏幕尺寸上始终以相同的比例显示Android View的位置和尺寸?

[英]How can I make Android View position and size be shown always with the same proportion on any screen size?

我尝试使用以下布局参数来定位按钮:

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Butt1"
    android:id="@+id/b1"
    android:rotation="-9"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="205dp" 

我的想法是稍后使按钮透明,并保持背景图像的视觉提示,以使用户知道那里有一个按钮。

我已经通过手动定位按钮和调整按钮的大小来使其在我的设备上工作,在我的设备上使用margin top 205dp我得到了:

这是我设备上的结果

在另一台设备上,按钮未与背景图像对齐/调整大小:

这是在另一台设备上

关于如何在任何设备上匹配按钮的位置和大小的想法?

使用<RelativeLayout> ,而不是android:layout_marginTop="205dp"使用android:layout_below属性,以便您可以在按钮名之后指定按钮!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/reminder" />
    <Spinner
        android:id="@+id/dates"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/times" />
    <Spinner
        android:id="@id/times"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentRight="true" />
    <Button
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/times"
        android:layout_alignParentRight="true"
        android:text="@string/done" />
</RelativeLayout>

Spinner EdiText位于EdiText之后,而Button位于第二个Spinner EdiText之后

如果您希望始终将按钮放在屏幕的左中间,请尝试使用此代码。

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true">

            <Button
                android:id="@+id/play"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <Button
                android:id="@+id/score"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/play"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/times" />
            <Button
                android:id="@id/times"
                android:layout_width="96dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/score"
                android:layout_alignParentRight="true" />
            <Button
                android:id="@+id/players"
                android:layout_width="96dp"
                android:layout_height="wrap_content"
                android:layout_below="@id/times"
                android:layout_alignParentRight="true"
                android:text="@string/done" />

        </LinearLayout>

    </RelativeLayout>

像容器一样使用LinearLayout并使用RelativeLayout的属性将其放在左侧(或右侧)和中心垂直,以便LinearLayout内的所有Button都可以从所有屏幕尺寸更改位置。

阅读我发布的文档 ,并查看RelativeLayout参数,以便您找到更适合您的解决方案。

义大利语: Credo tu sia italiano, android:layout_below定义专有的android:layout_below根据ID确定具体的要素,然后按ID marginTop 在使用LinearLayout进行维护时,请考虑使用RelativeLayout容器的专有名称,然后在schermata上进行分类。

一些相对布局指南参数

看看这个库:它使用百分比定位/大小调整功能定义了RelativeLayout版本。

我想指出的是,使用透明背景并让窗口的背景图像提示按钮是“简便”的方法,但这是一种不好的做法。 这样,您就不会向触摸您的用户的用户提供任何视觉反馈,您将被迫使用您的应用上传大图像,该大图像将不可避免地出现伪像,并且无法很好地适应不同的屏幕纵横比。

您应该尝试构建一个UI,其中按钮是实际按钮,位于屏幕上。

在您的情况下,这不是一件容易的事,因为您希望形状不规则的按钮相互重叠(如果您认为它们是矩形的)。 因此,您需要以特殊的/自定义方式处理触摸。

如果您决定采用其他路线,请查看以下其他问题和解答:

另一种方法是像在HTML map标签中那样用坐标映射图像。 这个 Android库似乎在Android Image Widget中实现了确切的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM