【问题标题】:Android XML ImageView below TextView but needs to align bottom with textViewTextView下方的Android XML ImageView但需要将底部与textView对齐
【发布时间】:2013-08-23 19:22:32
【问题描述】:

所以我的文本视图必须绘制在图像视图上,所以它在 xml 中定义如下:

    <ImageView
        android:id="@+id/chatBalloon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="-5dp"
        android:layout_marginRight="-5dp"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@+id/chatItemProfPic"
        android:scaleType="fitXY"
        android:src="@drawable/chat_bar_user" />

    <TextView
        android:id="@+id/userText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="3dp"
        android:layout_toRightOf="@+id/chatItemProfPic"
        android:text="username"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="15sp" />

但是我因为 textView 可以包含多行文本,所以我需要让 imageView 相应地增加它的高度。这将通过添加此规则来完成:

android:layout_alignBottom="idOfText"

但由于该部分尚未定义 textView,因此应用程序崩溃了。尝试通过 LayoutParams 中的 addRule 代码执行此操作时,我得到了相同的结果,因为我在绘制视图之前在 onCreate 中调用了它。

任何想法如何绕过这个?

已解决: 最终的 xml:

    <ImageView
        android:id="@+id/chatBalloon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="-5dp"
        android:layout_marginRight="-5dp"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@+id/chatItemProfPic"
        android:scaleType="fitXY"
        android:layout_alignBottom="@+id/userText"
        android:src="@drawable/chat_bar_user" />

    <TextView
        android:id="@id/userText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="3dp"
        android:layout_toRightOf="@+id/chatItemProfPic"
        android:text="username"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="15sp" />

【问题讨论】:

  • 请发布您的日志,

标签: android xml layout


【解决方案1】:

你使用

 android:layout_alignBottom="@+id/idOfText"

当您第一次引用特定 id 时。注意将 id 添加到资源标识符列表中的“+”。

然后,您可以不使用“+”将现有 id 分配给小部件,如下所示:

android:id="@id/idOfText"

之后。通常在我们分配 id 时创建它,这就是为什么您只需要关心相对布局中是否存在“+”。

在您的具体情况下:

<ImageView
    android:id="@+id/chatBalloon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="-5dp"
    android:layout_marginRight="-5dp"
    android:layout_marginTop="2dp"
    android:layout_toRightOf="@id/chatItemProfPic"
    android:layout_alignBottom="@+id/userText"
    android:scaleType="fitXY"
    android:src="@drawable/chat_bar_user" />

<TextView
    android:id="@id/userText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="7dp"
    android:layout_marginTop="3dp"
    android:layout_toRightOf="@id/chatItemProfPic"
    android:text="username"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textSize="15sp" />

您应该使用android:id = "@+id/chatItemProfPic" 设置个人资料图片小部件的 ID,假设您在对它的任何引用之前声明小部件。否则,类似地,使用“+”作为第一个引用,然后在声明不带“+”的小部件时分配 ID。

【讨论】:

  • 是的..这样解决了。不知道我可以从那里添加 id。谢谢!
  • 啊,请注意,使用“chatItemProfPic”,您每次引用它们时都会添加 ID。我本来预计它也会在那里崩溃......但通常你应该在布局中每个 ID 只有一次“+”(我认为更具体地说,如果它们在单独的视图树中,你只允许重复 ID)。我将编辑我的帖子。
【解决方案2】:

为什么不把那张图片像 textview 背景一样?

或类似的东西:

<RelativeLayout
            android:id="@+id/Rlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="35dp" >

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/img" />

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="lorem ipsum"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textSize="@dimen/dimension" />
        </RelativeLayout>

在相对布局中,您可以轻松地将文本放在图像视图上

【讨论】:

    猜你喜欢
    • 2013-03-26
    • 2018-06-30
    • 2017-12-03
    • 2020-03-25
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多