Android 如何适应不同手机屏幕上的元素比例
我想设置一些图像视图到一个特定的backgorund位置,它将设置在一种android手机上,比如Nexus4 但当其他安卓手机(如Nexus10)的屏幕改变时,它会改变位置 我在网上搜索查找解决方案,发现设置元素百分比是更好的方法,可以在不同的屏幕上设置相同的比例 我尝试使用不同的布局设计,如TableLayout、GridLayout、LinearLayout等,但无法同时设置所有anroid智能手机的位置 以下是我的RelativeLayout代码:Android 如何适应不同手机屏幕上的元素比例,android,xml,position,screen,Android,Xml,Position,Screen,我想设置一些图像视图到一个特定的backgorund位置,它将设置在一种android手机上,比如Nexus4 但当其他安卓手机(如Nexus10)的屏幕改变时,它会改变位置 我在网上搜索查找解决方案,发现设置元素百分比是更好的方法,可以在不同的屏幕上设置相同的比例 我尝试使用不同的布局设计,如TableLayout、GridLayout、LinearLayout等,但无法同时设置所有anroid智能手机的位置 以下是我的RelativeLayout代码: <RelativeLayou
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/test_backside"
>
<ImageView
android:layout_width="1920dp"
android:layout_height="1080dp"
android:id="@+id/bgTopLeftCorner"
android:src="@drawable/test_bg"
/>
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:src="@drawable/test_horizonal_layer"
android:layout_weight="30"
android:id="@+id/imageView5"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="44dp"
android:layout_marginStart="44dp" />
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/test_circle_1"
android:layout_marginRight="36dp"
android:layout_marginEnd="36dp"
android:id="@+id/imageView7"
android:layout_below="@+id/imageView5"
android:layout_toLeftOf="@+id/imageView6"
android:layout_toStartOf="@+id/imageView6" />
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/test_circle_2"
android:layout_weight="30"
android:id="@+id/imageView6"
android:layout_alignTop="@+id/imageView7"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/test_circle_3"
android:layout_weight="30"
android:id="@+id/imageView3"
android:layout_alignTop="@+id/imageView6"
android:layout_toLeftOf="@+id/imageView"
android:layout_toStartOf="@+id/imageView"
android:layout_marginRight="17dp"
android:layout_marginEnd="17dp" />
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:src="@drawable/test_vertical_layer"
android:layout_weight="30"
android:id="@+id/imageView"
android:layout_marginBottom="43dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
是的,正如您所说的设置百分比很好,在活动/片段类中找到您的屏幕宽度和高度,并通过将其宽度设置为(x*screenWidth)/100来设置相对布局中每个元素的宽度和高度 这会解决你的问题
对两幅图像使用相对布局。
Use the relative layout for 2 images.
Inside the layout on top of the first image, put the second (in this example uses LinearLayout)
<?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">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/image1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/image2" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="@drawable/image1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/image2" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
在第一个图像顶部的布局中,放置第二个图像(在本例中使用LinearLayout)
希望这能有所帮助,而这段代码可能会回答这个问题,提供关于如何以及为什么解决这个问题的信息,从而提高其长期价值