如何在屏幕的某些位置对齐视图,并在多种屏幕分辨率下保持一致?

2024-05-03

我在调整按钮时遇到了一些问题。我想把它们放在屏幕的(大约)1/3 和 2/3 处(我在下面提供了一个屏幕截图,以使事情更加清晰) 我的代码如下:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="@drawable/mainbg">

    <Button 
        android:text="@string/topbutton"
        android:background="@drawable/silvertablesbuttonbgkort"
        android:id="@+id/topbutton"
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center|center_vertical">
    </Button>

    <Button 
        android:text="@string/midbutton"
        android:background="@drawable/silvertablesbuttonbglang"
        android:id="@+id/midbutton"
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center|center_vertical">
   </Button>


<RelativeLayout 
    android:id="@+id/underbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button 
        android:text="@string/underbartekst"
        android:background="@drawable/silvertablesunderbar"
        android:id="@+id/underbarbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:textSize="20dp"
        android:gravity="left|center_vertical" >
    </Button>

</RelativeLayout>

我试图让它看起来像这样:(如果在这里发布屏幕截图模型不被允许,我很抱歉,我只是想不出更好的方法来澄清我正在尝试做的事情)

看来我无法发布图片,因为我在这里太新了......所以这里是屏幕截图的链接:https://i.stack.imgur.com/LIVup.png https://i.stack.imgur.com/LIVup.png

我最初的想法只是在两个按钮周围包裹一个垂直的线性布局,并在两个按钮之间放置空的文本视图,如果这个应用程序打算在一种屏幕尺寸上运行,那么这实际上是可行的,但我很确定它' d 在每部具有不同屏幕分辨率的手机上都会搞砸。我通过使用 android:layout_alignParentBottom="true" 的相对布局解决了底部栏的这个问题,但我真的想不出一种捕捉到 1/3rd 或 2/3rd 的方法。有没有办法做到这一点,可以适用于各种屏幕分辨率?

Edit:我解决了我的问题!我试图将其作为答案发布,但直到 8 小时后才能发布。然后就可以了,但现在,我将其作为编辑发布在这里:

我在屏幕中间放置了一个 0x0 的 TextView,并在其上方和下方放置了relativelayout,填充了屏幕。然后我将两个 0x0 的 TextView 放置在这些布局的中间,并在这些布局中放置两个新的relativelayout。一张位于最高 TextView 下方,一张位于最低 TextView 上方。我将按钮放在这些布局的中心。它的工作方式就像一个魅力,除了代码本身之外不依赖任何东西。这是我现在的代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="@drawable/mainbg">


    <TextView 
        android:text=" "
        android:id="@+id/ankermidden"
        android:layout_centerVertical="true"
        android:layout_width="0dp"
        android:layout_height="0dp">
    </TextView>

    <RelativeLayout
        android:id="@+id/ankerveldboven"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/ankermidden">

                <TextView 
                    android:text=" "
                    android:id="@+id/ankerboven"
                    android:layout_centerVertical="true"
                    android:layout_width="0dp"
                    android:layout_height="0dp">
                </TextView>

            <RelativeLayout
                android:id="@+id/ankerveldmidboven"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/ankerboven">

                 <Button 
                    android:text="@string/topbutton"
                    android:background="@drawable/silvertablesbuttonbgkort"
                    android:id="@+id/topbutton"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center|center_vertical">
                </Button>

            </RelativeLayout>

</RelativeLayout>


<RelativeLayout
        android:id="@+id/ankerveldonder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ankermidden">

    <TextView 
        android:text=" "
        android:id="@+id/ankeronder"
        android:layout_centerVertical="true"
        android:layout_width="0dp"
        android:layout_height="0dp">
    </TextView>

            <RelativeLayout
                android:id="@+id/ankerveldmidonder"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@id/ankeronder">      

                <Button 
                    android:text="@string/midbutton"
                    android:background="@drawable/silvertablesbuttonbglang"
                    android:id="@+id/midbutton"
                    android:layout_alignParentRight="true" 
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center|center_vertical">
                </Button>

            </RelativeLayout>       

 </RelativeLayout>

<RelativeLayout 
    android:id="@+id/underbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button 
        android:text="@string/underbartekst"
        android:background="@drawable/silvertablesunderbar"
        android:id="@+id/underbarbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:textSize="20dp"
        android:gravity="left|center_vertical" >
    </Button>

</RelativeLayout>

是的,这比我想象的要容易。


我找到了解决办法!一种完全相对于自身的,完全不依赖于像素或密度像素的。

我在屏幕中间放置了一个 0x0 的 TextView,并在其上方和下方放置了relativelayout,填充了屏幕。

然后我将两个 0x0 的 TextView 放置在这些布局的中间,并在这些布局中放置两个新的relativelayout。一张位于最高 TextView 下方,一张位于最低 TextView 上方。我将按钮放在这些布局的中心。

它的工作方式就像一个魅力,除了代码本身之外不依赖任何东西。

这是我现在的代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="@drawable/mainbg">


    <TextView 
        android:text=" "
        android:id="@+id/ankermidden"
        android:layout_centerVertical="true"
        android:layout_width="0dp"
        android:layout_height="0dp">
    </TextView>

    <RelativeLayout
        android:id="@+id/ankerveldboven"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/ankermidden">

                <TextView 
                    android:text=" "
                    android:id="@+id/ankerboven"
                    android:layout_centerVertical="true"
                    android:layout_width="0dp"
                    android:layout_height="0dp">
                </TextView>

            <RelativeLayout
                android:id="@+id/ankerveldmidboven"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/ankerboven">

                 <Button 
                    android:text="@string/topbutton"
                    android:background="@drawable/silvertablesbuttonbgkort"
                    android:id="@+id/topbutton"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center|center_vertical">
                </Button>

            </RelativeLayout>

</RelativeLayout>


<RelativeLayout
        android:id="@+id/ankerveldonder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ankermidden">

    <TextView 
        android:text=" "
        android:id="@+id/ankeronder"
        android:layout_centerVertical="true"
        android:layout_width="0dp"
        android:layout_height="0dp">
    </TextView>

            <RelativeLayout
                android:id="@+id/ankerveldmidonder"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@id/ankeronder">      

                <Button 
                    android:text="@string/midbutton"
                    android:background="@drawable/silvertablesbuttonbglang"
                    android:id="@+id/midbutton"
                    android:layout_alignParentRight="true" 
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center|center_vertical">
                </Button>

            </RelativeLayout>       

 </RelativeLayout>

<RelativeLayout 
    android:id="@+id/underbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button 
        android:text="@string/underbartekst"
        android:background="@drawable/silvertablesunderbar"
        android:id="@+id/underbarbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:textSize="20dp"
        android:gravity="left|center_vertical" >
    </Button>

</RelativeLayout>

是的,这比我想象的要容易。

Bill Gary 建议在 dp 中使用边距,这样可以在不同的屏幕尺寸上保持相同的比例,但经过大量实验后,在不同的屏幕上,事情最终对我来说又显得很奇怪。

在我回到这个问题之前,我会做更多的实验,因为这整个倾斜边缘的事情让我很奇怪......应该正确显示的东西没有,而那些不应该正确显示的东西,在一些屏幕分辨率。

我将开始做关于这些东西的作业,但就目前而言,尽管它有点长,但我上面发布的代码对我来说可以完美地工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在屏幕的某些位置对齐视图,并在多种屏幕分辨率下保持一致? 的相关文章

随机推荐

  • 无法在 TFS 中签入 UserControl.xaml 文件。接收错误:TF10169

    Visual Studio 2013 中的项目类型是桌面应用程序 我在该桌面应用程序中添加了 XAML 格式的用户控件 我使用了一些兼容性函数和库 以便 xaml 控件能够与简单的桌面应用程序集成 TFS 正在检查其他相关文件 但是当将 x
  • 计算素数并附加到列表

    我最近开始尝试使用 python 解决 Euler 项目的问题 并且在尝试计算素数并将其附加到列表中时遇到了这个障碍 我编写了以下代码 但我很困惑为什么它在运行时不输出任何内容 import math primes def isPrime
  • ShinyApp:由对等方重置连接

    我之前构建的闪亮应用程序在我的旧笔记本电脑上运行良好 最近我买了一台装有Windows10的新笔记本电脑 设置完所有内容后 我尝试运行该应用程序 但浏览器立即打开并关闭 并出现错误 正在收听http 127 0 0 1 5004 http
  • 从自定义类导入时,XMLBeans jar 无法签名

    在 NetBeans 中 我创建了一个 Exporter 类 该类使用 APACHE POI 将一些数据导出到 EXCEL 文件 而 APACHE POI 使用 XMLBeans 我通过下载 zip 二进制文件并手动添加 jar 来添加 A
  • C# SerialPort BaseStream ReadAsync - CancellationToken 从未取消?

    我尝试以异步方式从串行端口读取数据 请记住操作所花费的时间不得超过指定的时间段 我使用的代码 private async Task
  • unsafeInterleaveIO 什么时候不安全?

    与其他不安全 操作不同 文档 http hackage haskell org packages archive base latest doc html System IO Unsafe html v unsafeInterleaveIO
  • 如何在 SQL Server 2000 中传递大于 varchar(8000) 的字符串参数?

    如果将字符串参数定义为大小大于 8000 则会出现编译错误 e g The size 9000 given to the type varchar exceeds the maximum allowed for any data type
  • 使用 lambda 或 Stream API 合并流以生成交替序列

    我有一些按预期返回 Stream 的代码 但也许可以用某种类型的 lambda 或 stream 操作替换它 而不是耗尽 a 中的迭代器while loop 它只是一种交替流中元素的方法first and second当其中一个元素耗尽时停
  • 在 python 中计时时,我应该如何考虑 subprocess.Popen() 开销?

    编码社区的成员比我更聪明 我有一个 python 问题要问你们 我正在尝试优化一个 python 脚本 该脚本 除其他外 返回子进程执行和终止的挂钟时间 我想我已经接近这样的事情了 startTime time time process s
  • 对 Python 的 id() 感到困惑[重复]

    这个问题在这里已经有答案了 我可以理解以下定义 每个对象都有一个身份 类型和值 对象的身份 一旦创建就永远不会改变 你可能会认为它是 对象在内存中的地址 这is操作员比较身份 两个物体 这id 函数返回一个代表其值的整数 身份 我假设上面的
  • NodeJS:MySQL 有时会引发 ETIMEDOUT 错误

    我目前正在使用 NodeJS 开发一个应用程序 然而 经常服务器抛出这个错误 我无法与mysql交互 Error read ETIMEDOUT code ETIMEDOUT errno ETIMEDOUT syscall read fata
  • awk 比较多个文件

    我有2个文件 file1 1 apple 2 mango 3 banana 44 orange file2 1 apple 22 31 xyz 2 man 3 banana 44 oran 44 orange 我需要使用第 1 列和检查第
  • 产量回报延迟迭代问题

    我知道yield return 利用了延迟加载 但我想知道我是否可能滥用迭代器或者很可能需要重构 我的递归迭代器方法返回给定的所有祖先PageNode包括pageNode itself public class PageNodeIterat
  • Clojure / Noir:强制 HTTPS,如果请求是 http:// 则重定向到 https://

    我正在尝试在我的网站上强制使用 SSL 我想要一个环形中间件 将网站重定向到与 https 相同的 URL 如果它只是 http 我编写了以下代码 但除了检查请求方案并打印它应该重定向到的 URL 之外 它实际上没有做任何事情 defn h
  • 获取当前时间(以小时和分钟为单位)

    我正在尝试从系统收集信息 并且需要获取当前时间 以小时和分钟为单位 目前我有 date awk print 4 输出如下 16 18 54 怎样才能把秒数去掉呢 提供格式字符串 date H M Running man date将给出所有格
  • SQLITE 文件已加密或不是数据库

    我有一个大问题 我正在使用 SQLite 开发一个桌面应用程序 但在复制 粘贴过程中我断电并且该过程终止 因此数据库丢失 但是 我找到了恢复它的方法 但是数据库是加密的 当我尝试使用打开连接时conn Open 我收到错误 SQLITE 已
  • 在 Laravel 中使用 PUT/DELETE 有什么价值?

    用于将路由定义为资源Route resource 文档表明 Verb Path Action Route Name GET resource index resource index GET resource create create r
  • Rails:使用 RestClient 的外部 API 集成(未定义的局部变量或方法“user”)

    我正在建设一个数字图书馆 我已经完成了很多需要的功能 我目前在将数字图书馆与学习管理系统 LMS 集成时遇到问题 我已经有一个数字图书馆的管理员身份验证系统 使用设计宝石 我的目标是允许想要访问数字图书馆的用户使用其学习管理系统 LMS 凭
  • OSX AudioUnit SMP

    我想知道是否有人有编写利用多核处理器和 或对称多处理的 HAL AudioUnit 渲染回调的经验 我的场景如下 子类型的单个音频组件kAudioUnitSubType HALOutput 连同它的渲染回调 负责附加合成n具有独立的单独变化
  • 如何在屏幕的某些位置对齐视图,并在多种屏幕分辨率下保持一致?

    我在调整按钮时遇到了一些问题 我想把它们放在屏幕的 大约 1 3 和 2 3 处 我在下面提供了一个屏幕截图 以使事情更加清晰 我的代码如下