[英]How to make image distributed to screen width in RelativeLayout?
I have three icon the a RelativeLayout
, how to make them distributed to screen width? 我有三个图标a
RelativeLayout
,如何使它们分布到屏幕宽度?
Since I may add more icon to this line programmatically, I cannot hard code a fixed margin to these icon. 由于我可以通过编程方式向该行添加更多图标,因此无法为这些图标硬编码固定的边距。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/icon1"
android:src="@mipmap/icon" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/icon2"
android:layout_toRightOf="@id/icon1"
android:layout_alignBottom="@id/icon1"
android:src="@mipmap/icon" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/icon3"
android:layout_toRightOf="@id/icon2"
android:layout_alignBottom="@id/icon2"
android:src="@mipmap/icon" />
</RelativeLayout>
Please use LinearLayout
and use this code, which will work in all resolutions device. 请使用
LinearLayout
并使用此代码,该代码将在所有分辨率的设备上运行。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<ImageView
android:id="@+id/icon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/icon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/icon1"
android:layout_toRightOf="@id/icon1"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/icon3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/icon2"
android:layout_toRightOf="@id/icon2"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
Your code with small modification in attributes. 您的代码在属性上进行了少量修改。
Try this: 尝试这个:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/icon1"
android:src="@mipmap/icon" />
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/icon2"
android:layout_below="@id/icon1"
android:layout_alignParentBottom="@id/icon1"
android:src="@mipmap/icon" />
<ImageView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/icon3"
android:layout_below="@id/icon2"
android:layout_alignParentBottom="@id/icon2"
android:src="@mipmap/icon" />
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.