【问题标题】:How to put ImageView over RelativeLayout?如何将 ImageView 放在相对布局上?
【发布时间】:2018-08-29 05:44:33
【问题描述】:

我正在设计一个配置文件布局。我正在使用 Imageview 和 RelativeLayout。我正在尝试将 ImageView 放在 RelativeLayout 之上。我同时使用了海拔和 android:scaleType="centerCrop" 但它在设计编辑器中显示正常,但是当我编译并运行应用程序时,ImageView 总是停留在 RelativeLayout 后面。

在设计编辑器中显示

这是我想要的,但在设备中显示如下

请帮忙..

下面是我的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layout">


        <ImageView
            android:id="@+id/header_cover_image"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="#000000"
            android:scaleType="centerCrop"
            android:src="@drawable/nav_menu_heade" />


        <ImageView
            android:clickable="true"
            android:id="@+id/profile"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_below="@+id/header_cover_image"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="-130dp"
            android:scaleType="fitStart"
            android:elevation="8dp"
            android:padding="20dp"
            android:src="@drawable/passport" />


        <RelativeLayout
            android:id="@+id/profile_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/header_cover_image"
            android:background="#eb0772ca"
            android:elevation="2dp"
            android:paddingBottom="2dp">



            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="70dp"
                android:text="Sagar Rawal"
                android:textColor="#fff"
                android:textSize="24sp"
                android:textStyle="bold" />



            <TextView
                android:id="@+id/quote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/name"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="Don't Cry cuz it's over, Smile cuz it happen"
                android:textColor="#ffffff"
                android:textSize="18sp" />
            <TextView
                android:id="@+id/location"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/quote"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:text="Jumla, Nepal"
                android:textColor="#ffffff"
                android:textSize="16sp" />

        </RelativeLayout>



    </RelativeLayout>
</ScrollView>

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    将您的个人资料 ImageView 移到 RelativeLayout 下方,

    在大多数布局中(如 RelativeLayout 和 FrameLayout),z-index 由 添加项目的顺序

    您的 XML 应如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
    
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/layout">
    
    
            <ImageView
                android:id="@+id/header_cover_image"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="#000000"
                android:scaleType="centerCrop"
                android:src="@drawable/nav_menu_heade" />
    
    
            <RelativeLayout
                android:id="@+id/profile_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/header_cover_image"
                android:background="#eb0772ca"
                android:elevation="2dp"
                android:paddingBottom="2dp">
    
    
    
                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="70dp"
                    android:text="Sagar Rawal"
                    android:textColor="#fff"
                    android:textSize="24sp"
                    android:textStyle="bold" />
    
    
    
                <TextView
                    android:id="@+id/quote"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/name"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:text="Don't Cry cuz it's over, Smile cuz it happen"
                    android:textColor="#ffffff"
                    android:textSize="18sp" />
                <TextView
                    android:id="@+id/location"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/quote"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:text="Jumla, Nepal"
                    android:textColor="#ffffff"
                    android:textSize="16sp" />
    
            </RelativeLayout>
    
    
            <ImageView
                android:clickable="true"
                android:id="@+id/profile"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_below="@+id/header_cover_image"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="-130dp"
                android:scaleType="fitStart"
                android:elevation="8dp"
                android:padding="20dp"
                android:src="@drawable/passport" />
    
    
    
        </RelativeLayout>
    </ScrollView>
    

    【讨论】:

      【解决方案2】:

      <?xml version="1.0" encoding="utf-8"?>
      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
      
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/layout">
      
              <ImageView
                  android:id="@+id/header_cover_image"
                  android:layout_width="match_parent"
                  android:layout_height="150dp"
                  android:background="#000000"
                  android:scaleType="centerCrop"
                  android:src="@drawable/nav_menu_heade" />
      
              <RelativeLayout
                  android:id="@+id/profile_layout"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/header_cover_image"
                  android:background="#eb0772ca"
                  android:elevation="2dp"
                  android:paddingBottom="2dp">
      
                  <TextView
                      android:id="@+id/name"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerHorizontal="true"
                      android:layout_marginTop="70dp"
                      android:text="Sagar Rawal"
                      android:textColor="#fff"
                      android:textSize="24sp"
                      android:textStyle="bold" />
      
                  <TextView
                      android:id="@+id/quote"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/name"
                      android:layout_centerHorizontal="true"
                      android:layout_marginTop="10dp"
                      android:text="Don't Cry cuz it's over, Smile cuz it happen"
                      android:textColor="#ffffff"
                      android:textSize="18sp" />
                  <TextView
                      android:id="@+id/location"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_below="@+id/quote"
                      android:layout_centerHorizontal="true"
                      android:layout_marginTop="10dp"
                      android:text="Jumla, Nepal"
                      android:textColor="#ffffff"
                      android:textSize="16sp" />
      
              </RelativeLayout>
              
              //<ImageView should come below <RelativeLayout>
              <ImageView
                  android:clickable="true"
                  android:id="@+id/profile"
                  android:layout_width="200dp"
                  android:layout_height="200dp"
                  android:layout_below="@+id/header_cover_image"
                  android:layout_centerHorizontal="true"
                  android:layout_marginTop="-130dp"
                  android:scaleType="fitStart"
                  android:elevation="8dp"
                  android:padding="20dp"
                  android:src="@drawable/passport" />
          </RelativeLayout>
      </ScrollView>

      将个人资料&lt;ImageView/&gt; 放在&lt;RelativeLayout/&gt; 下方

      【讨论】:

        【解决方案3】:

        像这样编辑 Xml

        <?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
        
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/layout">
        
        
                <ImageView
                    android:id="@+id/header_cover_image"
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="#000000"
                    android:scaleType="centerCrop"
                    android:src="@drawable/back" />
        
        
                <RelativeLayout
                    android:id="@+id/profile_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/header_cover_image"
                    android:background="#eb0772ca"
                    android:elevation="2dp"
                    android:paddingBottom="2dp">
        
        
                    <TextView
                        android:id="@+id/name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="70dp"
                        android:text="Sagar Rawal"
                        android:textColor="#fff"
                        android:textSize="24sp"
                        android:textStyle="bold" />
        
        
                    <TextView
                        android:id="@+id/quote"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/name"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dp"
                        android:text="Don't Cry cuz it's over, Smile cuz it happen"
                        android:textColor="#ffffff"
                        android:textSize="18sp" />
        
                    <TextView
                        android:id="@+id/location"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/quote"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dp"
                        android:text="Jumla, Nepal"
                        android:textColor="#ffffff"
                        android:textSize="16sp" />
        
                </RelativeLayout>
        
                <ImageView
                    android:clickable="true"
                    android:id="@+id/profile"
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:layout_below="@+id/header_cover_image"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="-130dp"
                    android:scaleType="fitStart"
                    android:elevation="8dp"
                    android:padding="20dp"
                    android:src="@drawable/app_icon" />
        
        
            </RelativeLayout>
        </ScrollView>
        

        【讨论】:

          【解决方案4】:

          您可以尝试在要放在前面的视图上调用bringToFront()

          view.bringToFront();
          

          【讨论】:

            【解决方案5】:

            我看到你使用负边距,如果我是你,我从不使用负数作为边距,因为它会产生一些问题。请尝试这种方式:

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            
                <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            
                 <ImageView
                    android:clickable="true"
                    android:id="@+id/profile"
                    android:layout_width="200dp"
                    android:layout_height="200dp"
                    android:scaleType="fitStart"
                    android:elevation="8dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="20dp"
                    android:padding="20dp"
                    android:src="@drawable/ic_launcher_background" />
            
                 <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:weightSum="2">
            
                    <ImageView
                        android:id="@+id/header_cover_image"
                        android:layout_width="match_parent"
                        android:layout_height="150dp"
                        android:layout_weight="1"
                        android:background="#000000"
                        android:scaleType="centerCrop"
                        android:src="@drawable/ic_launcher_background" />
            
                    <RelativeLayout
                        android:id="@+id/profile_layout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="#eb0772ca"
                        android:elevation="2dp"
                        android:paddingBottom="2dp">
            
            
                        <TextView
                            android:id="@+id/name"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="70dp"
                            android:text="Sagar Rawal"
                            android:textColor="#fff"
                            android:textSize="24sp"
                            android:textStyle="bold" />
            
            
                        <TextView
                            android:id="@+id/quote"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/name"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="10dp"
                            android:text="Don't Cry cuz it's over, Smile cuz it happen"
                            android:textColor="#ffffff"
                            android:textSize="18sp" />
            
                        <TextView
                            android:id="@+id/location"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/quote"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="10dp"
                            android:text="Jumla, Nepal"
                            android:textColor="#ffffff"
                            android:textSize="16sp" />
            
                     </RelativeLayout>
                  </LinearLayout>
              </FrameLayout>
            

            【讨论】:

              【解决方案6】:

              几天前我也遇到过这类问题。但我用一些逻辑解决了这类问题。 这是我的 .xml 文件。希望你能理解。

              <ScrollView
                      android:fillViewport="true"
                      android:scrollbars="none"
                      android:layout_weight="1"
                      android:layout_width="match_parent"
                      android:layout_height="0dp">
              
              
                      <LinearLayout
                          android:orientation="vertical"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content">
                          <LinearLayout
                              android:orientation="vertical"
                              android:weightSum="10"
                              android:layout_width="match_parent"
                              android:layout_height="match_parent">
              
                              <RelativeLayout
                                  android:layout_weight="1.3"
                                  android:id="@+id/linear1"
                                  android:layout_width="match_parent"
                                  android:layout_height="0dp"
                                  android:background="@color/colorBackFilled">
                                  <com.prymepharm.android.CustomClass.MarkSimonsonRegulerTextView
                                      android:text="@string/pleaeEnterMobno"
                                      android:textColor="@color/colorWhite"
                                      android:layout_centerHorizontal="true"
                                      android:layout_marginTop="@dimen/twenty"
                                      android:textSize="@dimen/ssixteen"
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content" />
              
                              </RelativeLayout>
              
                              <RelativeLayout
                                  android:layout_weight="2.7"
                                  android:id="@+id/relative1"
                                  android:layout_width="match_parent"
                                  android:layout_height="0dp"
                                  android:background="@color/colorBackFilled">
              
                                  <LinearLayout
                                      android:orientation="vertical"
                                      android:layout_width="match_parent"
                                      android:layout_height="match_parent">
                                      <View
                                          android:layout_width="match_parent"
                                          android:layout_height="0dp"
                                          android:background="@color/colorBackFilled"
                                          android:layout_weight="1" />
                                      <View
                                          android:layout_width="match_parent"
                                          android:layout_height="0dp"
                                          android:background="@color/colorWhite"
                                          android:layout_weight="1" />
                                  </LinearLayout>
                                  <LinearLayout
                                      android:layout_centerInParent="true"
                                      android:layout_width="match_parent"
                                      android:layout_height="match_parent">
              
                                      <ImageView
                                          android:layout_width="match_parent"
                                          android:layout_height="match_parent"
                                          android:src="@drawable/phn96" />
                                  </LinearLayout>
                              </RelativeLayout>
                              <LinearLayout
                                  android:layout_weight="6"
                                  android:id="@+id/linear2"
                                  android:layout_width="match_parent"
                                  android:layout_height="0dp"
                                  android:background="@color/colorWhite"
                                  android:orientation="vertical">
                                  <com.prymepharm.android.CustomClass.MarkSimonsonSemiTextView
                                      android:id="@+id/phoneNum"
                                      android:layout_marginLeft="@dimen/thirty"
                                      android:layout_marginTop="@dimen/thirty_five"
                                      android:layout_marginStart="@dimen/thirty"
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"
                                      android:text="@string/phonenNum"
                                      android:textSize="@dimen/ssixteen" />
              
                                  <LinearLayout
                                      android:orientation="horizontal"
                                      android:layout_marginStart="@dimen/thirty"
                                      android:layout_marginEnd="@dimen/thirty"
                                      android:layout_width="match_parent"
                                      android:layout_height="@dimen/fifty"
                                      android:layout_marginTop="@dimen/eight"
                                      android:layout_marginBottom="@dimen/eight">
              
                                      <com.hbb20.CountryCodePicker
                                          android:id="@+id/ccp"
                                          android:layout_marginTop="@dimen/eight"
                                          app:ccp_areaCodeDetectedCountry="true"
                                          app:ccp_contentColor="@color/colorBackFilled"
                                          app:ccpDialog_showNameCode="false"
                                          android:layout_width="wrap_content"
                                          android:layout_height="match_parent" />
                                      <android.support.design.widget.TextInputLayout
                                          android:id="@+id/firstName"
                                          android:orientation="horizontal"
                                          android:layout_height="match_parent"
                                          android:layout_width="match_parent">
                                          <EditText
                                              android:id="@+id/entetPhoneNum"
                                              android:layout_width="match_parent"
                                              android:layout_height="match_parent"
                                              android:inputType="phone"
                                              android:maxLength="15"
                                              android:singleLine="true"
                                              android:hint="@string/typeInHere" />
                                      </android.support.design.widget.TextInputLayout>
                                  </LinearLayout>
                                  <com.prymepharm.android.CustomClass.MarkSimonsonSemiTextView
                                      android:id="@+id/send1"
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"
                                      android:layout_gravity="center_horizontal"
                                      android:layout_marginTop="@dimen/forty"
                                      android:textSize="@dimen/ssixteen"
                                      android:text="@string/send1"/>
              
                                  <com.prymepharm.android.CustomClass.MarkSimonsonSemiTextView
                                      android:id="@+id/send2"
                                      android:layout_marginTop="@dimen/ten"
                                      android:layout_gravity="center_horizontal"
                                      android:layout_width="wrap_content"
                                      android:layout_height="wrap_content"
                                      android:textSize="@dimen/ssixteen"
                                      android:text="@string/send2"/>
                                  <LinearLayout
                                      android:id="@+id/llbottom"
                                      android:orientation="vertical"
                                      android:layout_gravity="center"
                                      android:layout_width="@dimen/eighty"
                                      android:layout_marginBottom="@dimen/hundred"
                                      android:layout_marginTop="@dimen/forty"
                                      android:layout_height="@dimen/eighty">
                                      <ImageView
                                          android:id="@+id/button_round"
                                          android:layout_gravity="center_horizontal"
                                          android:layout_width="match_parent"
                                          android:layout_height="match_parent"
                                          android:src="@drawable/arn96" />
                                  </LinearLayout>
                              </LinearLayout>
                          </LinearLayout>
                      </LinearLayout>
                  </ScrollView>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-04-04
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-11-05
                相关资源
                最近更新 更多