【问题标题】:How to correctly position android layout without using Absolute Layout?如何在不使用绝对布局的情况下正确定位 android 布局?
【发布时间】:2012-07-27 02:25:20
【问题描述】:

对不起,这是一个菜鸟问题,但我在布局方面很烂。我将如何制作一个定位 flashlight_button 的布局,以便保持顶部和底部图像视图之间的比例。

在 nexus s (800x480) 上看起来不错,这就是我想要的

...图片...

但很明显,当我切换到 Galaxy nexus (1280 x 720) 时,它会将中间按钮移动到顶部,就像这样(因此它不会保留 nexus 上设计的顶部和底部边距之间的比率)

...图像...

布局在这里:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:padding="20dp" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="50dp"
    android:contentDescription="@string/application_logo_description"
    android:src="@drawable/mylight" />

<ImageButton
    android:id="@+id/settings_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@null"
    android:contentDescription="@string/settings_button_description"
    android:src="@drawable/settings_button" />

<ImageButton
    android:id="@+id/flashlight_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:background="@null"
    android:contentDescription="@string/flashlight_button_description"
    android:src="@drawable/flashlight_button_selector" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ad_button"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/powered_by_description"
    android:src="@drawable/powered_by" />

<ImageButton
    android:id="@+id/ad_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@null"
    android:contentDescription="@string/ad_button_description"
    android:src="@drawable/freeml" />

所以它只是将顶部和底部的图像视图锚定到父边缘,中间按钮只是使用 layout_maginBottom 按下。我正在为不同的密度和明显的密度像素提供缩放图像来表达尺寸,所以在更大的屏幕上似乎还不够。

这样做的正确方法是什么?不同的 .xml 文件或 ..?

//抱歉我还不能发图,希望你能想象出来。

【问题讨论】:

  • layout_centerInParent 是您正在寻找的属性,我相信。
  • 您能否发布实际图像而不是“...图像...”。目前尚不清楚最终结果应该是什么
  • 对不起,我是新来的,需要 10 个声望才能发布图片...@DeeV:我知道如何将其居中,但我不希望图像居中,它有点偏离顶部,这是我在 Photoshop 中制作的设计,我正在尝试实现这一点
  • @PavelDudka 谢谢,不过我仍然需要 1 分:-D

标签: android layout positioning


【解决方案1】:

您是否考虑过根据父母的中心放置您的手电筒图像? 所以现在你正在向下推手电筒图像(根据顶部图像位置)。使手电筒图像居中对齐并给它一些底部边距(根据您的设计将其推高一点)会更准确吗?

【讨论】:

  • 感谢您的回答,但这不起作用,即使底部边距 od 200dp,它也会保持居中
猜你喜欢
  • 1970-01-01
  • 2019-07-08
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多