如何在LinearLayout中的元素上重叠图像



我正试图在我的Android应用程序中创建一个教程,我想在一些UI元素上放置一些气球。我的UI构建在LinearLayouts上。

如何在LinearLayout上的特定位置(即id为"@id/email"的文本字段旁边)放置浮动图像。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" >
    <TextView android:id="@+id/email"
        style="@style/LoginBarText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3.0dp"
        android:text="Email:" />
</LinearLayout>

我知道唯一能够重叠元素的布局是RelativeLayout,但它不能覆盖其他布局中的元素。

感谢

您可以创建一个RelativeLayout并包含您的原始LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include 
            layout="@layout/layout_original" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />
    <include 
            layout="@layout/layout_overlay" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" />
</RelativeLayout>

或者,如果你只需要在元素旁边(而不是顶部)添加一个图像,试试这个:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" 
    android:orientation="horizontal">
    <ImageView android:id="@+id/image"
    android:src="@drawable/imagename"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="invisible" />
    <TextView android:id="@+id/email"
    style="@style/LoginBarText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3.0dp"
    android:text="Email:" />
</LinearLayout>

然后在活动中调用

image = (ImageView) findViewById(R.id.image);
image.setVisibility(View.VISIBLE);

最新更新