android - 如何使 ImageView float 在 LinearLayout 上方?

标签 android user-interface android-layout android-imageview

请检查下面图片的链接。由于声誉低下,我无法发布图片。

http://i.stack.imgur.com/nupZo.png

我的目标是获得像左边那样的布局,而我得到的是右边的布局。这是我的布局 xml 代码。

<?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:background="@color/metalDark"
android:orientation="vertical"
>
<LinearLayout
    android:id="@+id/topPanel"
    android:layout_width="fill_parent"
    android:layout_height="65dp"
    android:background="@color/metalList"
    android:elevation="6dp"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal">

    <TextView
        .../>


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/topPanel"
    android:layout_margin="10dp"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/albumheader"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@color/metalList"
        android:elevation="6dp">
        <LinearLayout
            android:id="@+id/underlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="25dp"
            android:layout_marginBottom="25dp"
            android:background="@drawable/canvas2">
        <ImageView
            android:id="@+id/al_art"
            android:layout_width="135dp"
            android:layout_height="135dp"
            android:layout_toLeftOf="@+id/al_details"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="-17dp"
            android:elevation="4dp"/>
        <LinearLayout
            android:id="@+id/al_details"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right|center"
            android:orientation="vertical">

            <TextView
                .../>
            <TextView
                .../>
            <TextView
                .../>

        </LinearLayout>
            </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="@color/metalList"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:elevation="6dp"
        >
        <ListView
            ..../>
    </LinearLayout>

</LinearLayout>

最佳答案

LinearLayout 不适合将 Views 放在彼此之上。

这篇文章对于进一步的信息非常有帮助: http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

这是我会做的:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <View
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_marginBottom="12dp"
        android:layout_height="?listPreferredItemHeight"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="12dp"
        android:layout_height="wrap_content">


        <ImageView
            android:id="@+id/img_orange_rectangle"
            android:background="#ff6600"
            android:layout_centerVertical="true"
            android:layout_width="match_parent"
            android:layout_height="128dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_centerVertical="true"
            android:text="hi I'm some text"
            android:gravity="end|top"
            android:padding="6dp"
            android:layout_alignStart="@id/img_orange_rectangle"
            android:layout_alignEnd="@id/img_orange_rectangle"
            android:layout_alignBottom="@id/img_orange_rectangle"
            android:layout_alignTop="@id/img_orange_rectangle"
            android:layout_height="0dp"/>


        <ImageView
            android:id="@+id/img_blue_square"
            android:background="#2541ba"
            android:layout_marginStart="24dp"
            android:layout_centerVertical="true"
            android:layout_width="156dp"
            android:layout_height="156dp"/>
    </RelativeLayout>


    <ListView
        android:id="@+id/listview"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="12dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

根据以下评论于 2015 年 8 月 15 日更新:

But please tell me why have you kept the layout_height of the RelativeLayout as 0?

我在 RelativeLayout 上使用了 0 的高度,因为布局根据 layout_weight 获取高度。

And will the height of orange rectangle change according to device and screen size?

没有。尽管我更新了布局以能够这样做。我推荐this有关如何支持多种屏幕尺寸的文章。往这底部this StackOverflow post第 2 部分讨论了来自 Google IO pdf 的每个屏幕尺寸的尺寸 xml。

Also if I want to put texts inside the orange rectangle, how do I ensure that it remains within it?

我在上面添加了另一个布局。有多种方法可以解决这个问题。我选择的方法是制作一个 TextView 来对齐其上方 ImageView 的属性。

关于android - 如何使 ImageView float 在 LinearLayout 上方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023535/

相关文章:

javascript - 使用 HTML/CSS/JavaScript 在 Web 应用程序中提供 PyQt4 GUI

user-interface - 使用字符串在 Red 语言中创建单词和路径

android - 添加 lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER) 不适用于立方折线图

android - 在 android 上的 LinearLayout 上设置 "z-index"

android - 如何在图库上叠加 ImageView ?

android - Intent 中的地理在 Android 应用程序中不起作用

Android:无法实例化 Activity ComponentInfo

Android Studio 在 string.xml 文件中显示印地语方框

javascript - 如果内容没有改变,如何防止 $.POST?

android - 使用 SimpleDateFormat 格式化日期时出现 NullPointerException