【问题标题】:Same absolute position in different screen size Android imageview不同屏幕尺寸Android imageview中的绝对位置相同
【发布时间】:2016-03-29 11:54:49
【问题描述】:

如何在不同的屏幕尺寸下拥有相同的 imageview 绝对位置?

  <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent" android:layout_height="match_parent"  android:background="#000000">
        <ImageView
           android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            android:id="@+id/road"
            android:src="@drawable/road" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/car"
            android:src="@drawable/car"
            android:layout_marginLeft="112dp"
            android:layout_marginTop="168dp" />
    </RelativeLayout>

不同屏幕尺寸的汽车位置不一样。

【问题讨论】:

    标签: android imageview position screen absolute


    【解决方案1】:

    你可以使用谷歌库PercentRelativeLayout这个library你可以通过procentege设置widthheightma​​rgin这很棒,因为在所有屏幕上它们看起来都一样,当然编码也不难。这里的例子:

    <android.support.percent.PercentRelativeLayout
             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
             app:layout_widthPercent="50%"
             app:layout_heightPercent="50%"
             app:layout_marginTopPercent="25%"
             app:layout_marginLeftPercent="25%"/>
     </android.support.percent.PercentRelativeLayout>
    

    你必须在你的 build.gradle 中添加这一行

    dependencies {
        compile 'com.android.support:percent:23.2.0'
    }
    

    Google 官方文档 https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

    希望这对您的案例有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多