【问题标题】:Why is there an empty space on the right of my RelativeLayout?为什么我的 RelativeLayout 右侧有一个空白区域?
【发布时间】:2012-08-30 21:58:56
【问题描述】:

我有两个相同、大小相同的 RelativeLayout,其中包含一些文本和一个我想与右侧对齐的图像。然而,似乎有某种填充物不允许我把它放在右边,即使我从未指定过这一点。

这是我的代码:

<LinearLayout
    android:baselineAligned="false"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/my_relativelayout"
        android:background="@drawable/my_background"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="right">
        <TextView
            android:id="@+id/my_textview"
            android:textAppearance="?android:attr/textAppearanceButton"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <ImageView
            android:id="@+id/my_imageview"
            android:layout_centerVertical="true"
            android:scaleType="center"
            android:src="@drawable/my_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/my_textview">
        </ImageView>
    </RelativeLayout>

    <RelativeLayout
        ... same thing here ...>
    </RelativeLayout>

</LinearLayout>

编辑:空白空间不是另一个RelativeLayout。另一个RelativeLayout是另一个带有文本和图像的“蓝色矩形”,并且也遇到了右侧空间的问题。像这样:

【问题讨论】:

  • 这个空间是因为你的另一个。里面有什么?
  • @iturki,不,LinearLayout中有两个相同的RelativeLayout,每个的权重为1。即,每个占用一半的屏幕空间。这两个 RelativeLayouts 在右侧都有空间。

标签: android android-layout android-relativelayout gravity


【解决方案1】:

这是因为您使用的是android:layout_toRightOf 属性。这会将当前视图的左边缘与您正在引用的视图的右边缘 (my_textview) 对齐。如果您希望视图的右侧与父视图的右侧匹配,请尝试使用android:layout_alignParentRight="true"

【讨论】:

  • 首先,这并没有改变任何东西:(其次,我不只是希望图像在右侧;我希望文本和图像在一起,它们之间没有空格.
【解决方案2】:

试试这个布局:

    <ImageView
        android:id="@+id/my_imageview"
        android:layout_centerVertical="true"
        android:scaleType="center"
        android:src="@drawable/my_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:Layout_alignParentRight="true">
    <TextView
        android:id="@+id/my_textview"
        android:textAppearance="?android:attr/textAppearanceButton"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/my_imageview">
    </TextView>

【讨论】:

  • 这行不通,你不能把视图 toLeftOf 本身。我认为您的意思是toLeftOf="my_imageview",在这种情况下,我相信您需要在 TextView 之前声明 ImageView。如果您引用尚未在该文件中声明的 ID,您将崩溃或编译器会抱怨。
  • 已编辑。虽然我认为顺序在引用中并不重要。
  • 你是对的! developer.android.com/guide/topics/ui/layout/relative.html 定位视图部分的最后一段。我的立场是正确的,尽管我可以发誓我过去曾遇到过这样的问题。
  • @MattDavis 感谢分享。但是,您应该避免混淆并遵守顺序。
【解决方案3】:

检查您在图像视图中使用的图像的边界(在图像编辑器中)。看起来图像中有一些透明区域导致了混乱/问题。

如果这不是问题:试试这个 为ImageView 添加属性alignParentRight="true" 代替 android:gravity="right" 不适用于 RelativeLayouts

【讨论】:

    【解决方案4】:

    最终奏效的解决方案是将

    android:paddingRight="0dip"
    

    在每个相对布局中,奇怪的是,虽然我从未说过应该有任何填充。几天来,我一直在努力解决这个问题;谢谢大家的意见!

    【讨论】:

      【解决方案5】:

      因为您的LinearLayout 具有水平布局android:orientation="horizontal",所以该空间将被第一个RelativeLayout 旁边的任何对象占用,在您的情况下,它看起来像第二个RelativeLayout

      如果这没有帮助,我会尝试调整这两个相对布局的 layout_weight 和 layout_width 属性。

      【讨论】:

      • 不,LinearLayout中有两个相同的RelativeLayout,每个的权重都是1。也就是说,每个都占一半的屏幕空间。这两个RelativeLayouts都有一个TextView和ImageView,以及右边的空间。
      • 为什么投反对票?您的问题不够清楚,以至于我们中的一些人认为第二个相对布局是右侧的空间。很高兴您编辑了帖子以使其更清晰。
      猜你喜欢
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多