【问题标题】:How can I use RelativeLayout to align buttons to the right of an ImageView如何使用 RelativeLayout 将按钮对齐到 ImageView 的右侧
【发布时间】:2014-12-19 06:37:14
【问题描述】:

我正在为一个简单的相对布局而苦苦挣扎。它应该在左侧有一个图像,在右侧有一列其他视图。垂直对齐是正确的,但水平对齐对于图像左侧的按钮非常令人费解,即使我要求它们位于右侧。
谁能解释一下:

  • 为什么会这样,
  • 如何实现我想要的排列方式。

这是布局xml和截图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/widget51"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

  <ImageView
      android:id="@+id/ivLove"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:layout_centerHorizontal="true"
      />

  <TextView
      android:id="@+id/tvDate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:hint="Image name"
      android:layout_toRightOf="@+id/ivImage"
      />

  <TextView
      android:id="@+id/tvPage"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:hint="Page URL"
      android:layout_below="@+id/tvDate"
      android:layout_toRightOf="@+id/ivImage"
      />

  <Button
      android:id="@+id/btnNext"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Next"
      android:layout_below="@+id/tvPage"
      android:layout_toRightOf="@+id/ivImage"
      />

  <Button
      android:id="@+id/btnPrevious"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Previous"
      android:layout_below="@+id/btnNext"
      android:layout_toRightOf="@+id/ivImage"
      />

</RelativeLayout>

如果您对图片有疑问,请参阅Android : Simple HTML parsing & image downloader using AsyncTask

【问题讨论】:

    标签: android android-layout alignment android-relativelayout


    【解决方案1】:

    您将这些控件(TextViewButton)放在 ID 为 ivImageImageView 的右侧。

    但是,您的布局不包含任何具有该 ID 的 ImageView(但有一个具有 ivLove 的 ImageView)。 控件出现在左侧是因为这是 RelativeLayout 内视图的默认停靠。

    尝试更新您的参考资料(将每个“ivImage”替换为“ivLove”。

    注意——如果你使用 Eclipse,你没有得到编译错误的原因是你的项目中可能有一个 ImageViewandroid:id="ivImage"。启用(并且可能更新)lint 将帮助您过滤此类问题。

    【讨论】:

    • 看在上帝的份上,太尴尬了!我至少 100% 确定我已经检查过这类问题。感谢您指出。现在它起作用了。我正在使用带有来自 Android Studio 的编译器的 Emacs 和 cedet,我尝试在(lint:版本 23.0.5)xml 文件上使用 lint,但它没有找到任何东西(或者我可能不明白如何驱动 lint) .
    • 别不好意思,我们都是这样学习的。 [Lint 是一个漂亮的工具,但它会让你变得懒惰并且对错别字不太敏感。]
    • 我知道这是我们学习的方式,但这很尴尬,因为我已经编码了 40 多年,其中 30 年是我工作的一部分或全部。我唯一的辩护是我对 Android 很陌生,但我仍然应该看到这一点。顺便说一句,感谢您的快速响应。
    猜你喜欢
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2015-03-17
    • 1970-01-01
    • 2017-01-25
    相关资源
    最近更新 更多