【问题标题】:How to set image and text right top of relative layout?如何在相对布局的右上方设置图像和文本?
【发布时间】:2018-01-25 17:31:42
【问题描述】:
 <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_alignParentTop="true"
           android:layout_centerHorizontal="true"
           android:layout_marginLeft="20dp"
           android:layout_marginRight="20dp"
           android:layout_marginTop="20dp"
           >

           <TextView
               android:id="@+id/nameText"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:ems="10"
               android:fontFamily="sans-serif-medium"
               android:letterSpacing="-0.03"
               android:text="Ajay Kulkarni"
               android:textColor="#3e4360"
               android:textSize="16sp"
               android:textStyle="normal"/>
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_below="@+id/nameText"
               android:fontFamily="sans-serif"
               android:letterSpacing="-0.03"
               android:text="Co-Founder"
               android:gravity="right"
               android:textColor="@color/bluey_grey_three"
               android:textSize="12sp"
               android:textStyle="normal"/>

           <com.kdcos.contsync.utilities.RoundedImageView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentEnd="true"

               android:layout_marginEnd="32dp"
               android:layout_toStartOf="@+id/imageView3"
               android:background="@drawable/ic_success_illustration" />

           <com.kdcos.contsync.utilities.RoundedImageView
               android:layout_width="64dp"
               android:layout_height="64dp"
               android:layout_alignParentEnd="true"
               android:layout_marginEnd="29dp"
               android:background="@drawable/ic_cards_active"
               android:id="@+id/imageView3" />

           <com.kdcos.contsync.utilities.RoundedImageView
               android:layout_width="64dp"
               android:layout_height="64dp"
               android:layout_alignParentEnd="true"
               android:layout_marginEnd="29dp"
               android:layout_toLeftOf="@+id/imageView3"
               android:background="@drawable/ic_cards_active"
               android:id="@+id/imageView4" />
       </RelativeLayout>

这是我的 xml,我想在 android 中设置所有 2 textview 2 imageview 相对布局的权利。我的预期屏幕如下所示,请建议我如何在相对布局的右上角设置 textview 和 imageview。

使用给定的代码,我可以在右侧保留一个 imageview,我同时保留了 textview 和一个 imageview。

【问题讨论】:

  • 您期望的屏幕在哪里?
  • @iamgopal 给定的解决方案是正确的,但我们必须在给定图像视图的左侧显示一个我无法设置的图像视图

标签: android android-relativelayout


【解决方案1】:

使用此代码。干杯。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp">

    <TextView
        android:id="@+id/nameText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toStartOf="@id/imageView3"
        android:ems="10"
        android:fontFamily="sans-serif-medium"
        android:gravity="end"
        android:letterSpacing="-0.03"
        android:text="Ajay Kulkarni"
        android:textColor="#3e4360"
        android:textSize="16sp"
        android:textStyle="normal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/nameText"
        android:fontFamily="sans-serif"
        android:gravity="end"
        android:layout_toStartOf="@id/imageView3"
        android:letterSpacing="-0.03"
        android:text="Co-Founder"
        android:textSize="12sp"
        android:textStyle="normal" />

    <com.kdcos.contsync.utilities.RoundedImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentEnd="true"

           android:layout_marginEnd="32dp"
           android:layout_toStartOf="@+id/imageView3"
           android:background="@drawable/ic_success_illustration" />

       <com.kdcos.contsync.utilities.RoundedImageView
           android:layout_width="64dp"
           android:layout_height="64dp"
           android:layout_alignParentEnd="true"
           android:layout_marginEnd="29dp"
           android:background="@drawable/ic_cards_active"
           android:id="@+id/imageView3" />

       <com.kdcos.contsync.utilities.RoundedImageView
           android:layout_width="64dp"
           android:layout_height="64dp"
           android:layout_alignParentEnd="true"
           android:layout_marginEnd="29dp"
           android:layout_toLeftOf="@+id/imageView3"
           android:background="@drawable/ic_cards_active"
           android:id="@+id/imageView4" />

</RelativeLayout>

【讨论】:

  • 但是我们有两个重叠的图像视图,我需要将它隔开,就像我在预期的屏幕中看到的圆形一样,
【解决方案2】:

试试这个

<?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">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">

          <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="vertical"
                android:layout_weight=".1">



                <TextView
                    android:id="@+id/nameText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:fontFamily="sans-serif-medium"
                    android:letterSpacing="-0.03"
                    android:text="Ajay Kulkarni"
                    android:textColor="#3e4360"
                    android:gravity="right"
                    android:textSize="16sp"
                    android:textStyle="normal"/>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/nameText"
                    android:fontFamily="sans-serif"
                    android:letterSpacing="-0.03"
                    android:text="Co-Founder"
                    android:gravity="right"
                    android:textColor="@color/cardview_dark_background"
                    android:textSize="12sp"
                    android:textStyle="normal"/>

            </RelativeLayout>


        <ImageView
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:scaleType="fitXY"
            android:id="@+id/imgone"
            android:tint="@color/colorPrimary"
            android:src="@mipmap/ic_camera_white_48dp"
            android:layout_weight=".1"/>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:tint="@color/colorPrimary"
            android:id="@+id/imagetow"
            android:scaleType="fitXY"
            android:src="@mipmap/ic_camera_white_48dp"
            android:layout_weight=".1"/>


    </LinearLayout>




</RelativeLayout>

输出

【讨论】:

  • thanx 但请检查我的预期输出是否正确,只有我们想要右侧文本和图像视图,但在给定的解决方案中,它即将出现左侧文本视图
  • 不,imgopal 给出的解决方案是正确的,我需要在第一个图像视图的左侧或右侧添加一个图像视图
  • @ManojKumar 你的意思是像 Imageview>Textview>ImageView 或发布你的确切屏幕我更正它
  • @ManojKumar 正如你的豪宅 expectedoutput 一个 Textview 右侧两个 Imageview ......所以我已经根据你的预期输出完成了
猜你喜欢
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多