Placing/Overlapping(z-index) a view above another view in android

前端 未结 11 1573
遥遥无期
遥遥无期 2020-11-22 17:07

I have a linear layout which consists of imageview and textview , one below another in a linear layout.



        
相关标签:
11条回答
  • 2020-11-22 17:35

    There is a way to use LinearLayout. Just set the marginTop of your previous element to the corresponding negative value, and make sure the element you want on top is after the element you want below in your XML.

    <linearLayout android:orientation="horizontal" ... >
    <ImageView 
     android:id="@+id/thumbnail"
     android:layout_weight="0.8" 
     android:layout_width="0dip"
     android:layout_height="fill_parent"
    >
    </ImageView>
    <TextView 
    android:id="@+id/description"
    android:layout_marginTop="-20dip"
    android:layout_weight="0.2"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    >
    </TextView>
    
    0 讨论(0)
  • 2020-11-22 17:39

    You can't use a LinearLayout for this, but you can use a FrameLayout. In a FrameLayout, the z-index is defined by the order in which the items are added, for example:

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/my_drawable"
            android:scaleType="fitCenter"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:padding="5dp"
            android:text="My Label"
            />
    </FrameLayout>
    

    In this instance, the TextView would be drawn on top of the ImageView, along the bottom center of the image.

    0 讨论(0)
  • 2020-11-22 17:40

    AFAIK you cannot do it with linear layouts, you'll have to go for a RelativeLayout.

    0 讨论(0)
  • 2020-11-22 17:42

    If you are adding the View programmatically, you can use yourLayout.addView(view, 1);

    where 1 is the index.

    0 讨论(0)
  • 2020-11-22 17:46

    Give a try to .bringToFront():

    http://developer.android.com/reference/android/view/View.html#bringToFront%28%29

    0 讨论(0)
  • 2020-11-22 17:47

    I solved the same problem by add android:elevation="1dp" to which view you want it over another. But it can't display below 5.0, and it will have a little shadow, if you can accept it, it's OK.

    So, the most correct solution which is @kcoppock said.

    0 讨论(0)
提交回复
热议问题