简体   繁体   English

如何在RelativeLayout中将图像分配到屏幕宽度?

[英]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.

相关问题 如何将RelativeLayout的宽度设置为屏幕宽度? - How I set the width of RelativeLayout to screen width? Android:如何将RelativeLayout填充到整个屏幕宽度? - Android: How to fill RelativeLayout to full width of screen? 设置relativelayout的宽度为屏幕宽度的1/3? - Set the width of relativelayout 1/3 the width of the screen? 如何在android中的relativelayout的宽度上转换图像 - How to translate image across the width of relativelayout in android 如何使RelativeLayout不能占据整个屏幕? - How to make RelativeLayout not take up the whole screen? RelativeLayout中的按钮填充屏幕宽度 - Buttons in RelativeLayout fill screen width 如何设置RelativeLayout内部的LinearLayout的宽度与屏幕分辨率无关? - How to set the width of LinearLayout inside RelativeLayout to be independent of screen resolution? 如何使带有多个子布局的RelativeLayout的ScrollView充满屏幕? - How to make ScrollView with a RelativeLayout with multiple child layouts fill the screen? Android relativeLayout:如何使按钮占据屏幕底部? - Android relativeLayout: How to make button take bottom port of screen? 在屏幕宽度的50%的RelativeLayout中加载WebView时出现问题 - Problems loading a WebView in a RelativeLayout with the 50% of the width of the screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM