【问题标题】:Different position on different screen size不同屏幕尺寸的不同位置
【发布时间】:2018-10-26 10:44:43
【问题描述】:

我想在图像的特定位置放置文本、按钮或类似的东西。但由于设备具有不同的屏幕分辨率,文本会改变位置并且不会保留在我放置的位置。

这是对 Nexus 4 的看法:

这是 Pixel 2 XL 的视图

我该如何解决这个问题?

tuner_activity.xml

<?xml version="1.0" encoding="utf-8"?>
  <android.support.constraint.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/manico"
    android:contentDescription="@string/chords" />

  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="81dp"
    android:layout_marginStart="81dp"
    android:layout_marginTop="100dp"
    android:text="D"
    android:textColor="@color/black"
    android:textSize="35sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/imageView" />

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="180dp"
    android:layout_marginStart="180dp"
    android:layout_marginTop="100dp"
    android:text="G"
    android:textColor="@color/black"
    android:textSize="35sp"
    app:layout_constraintStart_toEndOf="@+id/textView"
    app:layout_constraintTop_toTopOf="@+id/imageView" />
    </android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 这在Android中很正常,因为它支持different screen sizes
  • 请提供您的布局
  • 这里有一个有用的link ImageView 属性
  • 请提供您使用过的xml和java代码。
  • 这会有点棘手,因为你想把东西放在你的背景图片上。这些针脚(Idk 他们叫什么)占用多少空间以及它们从顶部/彼此之间有多少空间等是否有任何规则?如果是这样,您可以使用 ConstraintLayout 并将 Guidelines 设置为某些值,例如到每排引脚的中间,然后在此基础上放置textviews

标签: java android android-layout


【解决方案1】:

似乎您使用match_parent 表示ImageView 宽度、高度。 您的 ImageView 将根据设备屏幕尺寸拉伸/倾斜,因此您无法相应地移动 ButtonView。

但是,在这种情况下,您事先知道图像尺寸和纵横比,您应该根据这些尺寸(而不是 match_parent)创建您的 ImageView。现在 ImageView 将始终保持纵横比和大小,您可以相应地定位您的TextView,ButtonView

【讨论】:

    猜你喜欢
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多