简体   繁体   English

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

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

I've tried position the button with this layout parameters: 我尝试使用以下布局参数来定位按钮:

    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" 

My idea is to make the button transparent later and keep the visual hint of the background image to let the user know there is a button there. 我的想法是稍后使按钮透明,并保持背景图像的视觉提示,以使用户知道那里有一个按钮。

I've already made it work on my device by manually positioning and sizing the button, on my device using margin top 205dp I get this: 我已经通过手动定位按钮和调整按钮的大小来使其在我的设备上工作,在我的设备上使用margin top 205dp我得到了:

这是我设备上的结果

On another device the buttons are not aligned / sized as the background image: 在另一台设备上,按钮未与背景图像对齐/调整大小:

这是在另一台设备上

Any idea on how I can match position and size of the button on any device? 关于如何在任何设备上匹配按钮的位置和大小的想法?

Use a <RelativeLayout> and instead of android:layout_marginTop="205dp" use the property android:layout_below so you can specify the Button after who it is! 使用<RelativeLayout> ,而不是android:layout_marginTop="205dp"使用android:layout_below属性,以便您可以在按钮名之后指定按钮!

Example

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

The Spinner is after the EdiText and the Button is after the second Spinner ALWAYS Spinner EdiText位于EdiText之后,而Button位于第二个Spinner EdiText之后

If you want always your button in the left-center of your screen try with this code. 如果您希望始终将按钮放在屏幕的左中间,请尝试使用此代码。

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

Use a LinearLayout like a container and with the property of the RelativeLayout put it at the left(or right) and center-vertical so all the Buttons inside the LinearLayout change position from all screens dimension. 像容器一样使用LinearLayout并使用RelativeLayout的属性将其放在左侧(或右侧)和中心垂直,以便LinearLayout内的所有Button都可以从所有屏幕尺寸更改位置。

Read the documents that i post and see the RelativeLayout paramenter so you can find the solution that it's better for you. 阅读我发布的文档 ,并查看RelativeLayout参数,以便您找到更适合您的解决方案。

Italian: Credo tu sia italiano, se usi la proprietà android:layout_below dici praticamente che l'elemento con questa proprietà si deve posizionare subito sotto un determinato elemento specificato per ID Poi se vuoi li spazi l'uno dall'altro con un marginTop . 义大利语: Credo tu sia italiano, android:layout_below定义专有的android:layout_below根据ID确定具体的要素,然后按ID marginTop Per mantenere il tutto centrato usa un LinearLayout, consideralo un container che ha come proprietà del RelativeLayout quelle di stare a sinistra e al centro della schermata, così si adatta ad ogni schermo. 在使用LinearLayout进行维护时,请考虑使用RelativeLayout容器的专有名称,然后在schermata上进行分类。

Some Relative Layout guide And parameters 一些相对布局指南参数

Check out this library: it define version of the RelativeLayout with percent positioning / sizing ability. 看看这个库:它使用百分比定位/大小调整功能定义了RelativeLayout版本。

I want to point out that using a transparent background and letting the button be hinted by the background image of your window is the "easy" way but is a bad practice. 我想指出的是,使用透明背景并让窗口的背景图像提示按钮是“简便”的方法,但这是一种不好的做法。 That way you give no visual feedback to the user touching your UI, you are forced to upload a big image with your App that will be stretched with inevitable artifacts and doesn't really well adapt to different screen aspect ratio. 这样,您就不会向触摸您的用户的用户提供任何视觉反馈,您将被迫使用您的应用上传大图像,该大图像将不可避免地出现伪像,并且无法很好地适应不同的屏幕纵横比。

You should try to build an UI where the button is an actual button, positioned in your screen. 您应该尝试构建一个UI,其中按钮是实际按钮,位于屏幕上。

In your case this isn't an easy task because you want irregular shaped buttons that overlap with each other (if you consider them rectangular). 在您的情况下,这不是一件容易的事,因为您希望形状不规则的按钮相互重叠(如果您认为它们是矩形的)。 Thus you need to handle touch in a special /custom way. 因此,您需要以特殊的/自定义方式处理触摸。

If you decide to go with this other route check out this other questions and answers: 如果您决定采用其他路线,请查看以下其他问题和解答:

Another way can be to map the image with coordinates like you would do in HTML map tag. 另一种方法是像在HTML map标签中那样用坐标映射图像。 This Android Library seems to implement that exact behavior in an Android Image Widget. 这个 Android库似乎在Android Image Widget中实现了确切的行为。

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

相关问题 Android如何评估视图大小以在所有设备中保持相同比例 - Android how to evaluate view size to keep same proportion in all devices 如何调整图像大小以在Android中呈现相同比例 - How to size images to render the same proportion in Android Android布局-将ImageButtons放置在屏幕的相等象限中,并按比例调整它们的大小 - android layout - position ImageButtons in equal quadrants of the screen and size them to proportion 我如何在Android中使用相同的图像为不同的屏幕尺寸? - how can i use same image for different screen size in android? 如何在Android中调整文字以适应屏幕尺寸? - How can I make text adjust to the size of the screen in android? 如何使View的大小与我自定义的TextureVIew Android的大小相同 - How to make the size of View the same as the size of my custom TextureVIew Android 在任何Android屏幕上显示相同的字体大小 - Displaying same font size in any Android screen Android:使自定义视图的尺寸大于屏幕的尺寸 - Android: make the size of a custom view bigger than the size of screen 如何为imageView设置最终大小(50dp),以便在任何android设备屏幕上看起来都一样大小 - how to set final size(50dp) for an imageView so that it looks same size on any android device screen 如何在 Android Studio 的每台设备上使视图大小相同 - How to make view size the same on every device on Android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM