【问题标题】:Allign view in relativelayout to borders将相对布局中的视图与边框对齐
【发布时间】:2014-02-02 18:32:07
【问题描述】:

有没有办法在边框旁边的相对布局内布局视图。

我有以下布局(简化的测试用例),我想让图像尽可能向右移动,使其与右边框对齐。有没有什么布局方法可以做到这一点?

<RelativeLayout android:background="#ff0000" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<EditText android:layout_width='200dp' 
android:layout_height='wrap_content' 
android:text='First line' 
android:id='@+id/first' />

<ImageView android:src='@drawable/test_grid16x16' 
android:layout_width="16dp" 
android:layout_height="16dp"
android:layout_toRightOf ='@id/first'/>

</RelativeLayout>

【问题讨论】:

  • fill_parent 已弃用,请改用 match_parent。
  • fill_parent 已弃用。

标签: android layout view android-relativelayout


【解决方案1】:

在 RelativeLayout 中,孩子可以将这些属性设置为 true 以与其容器的边框对齐:alignParentTopalignParentBottomalignParentLeftalignParentRight;并且可以组合。

在您的情况下:将 alignParentRight="true" 添加到您的 ImageView。

我也会移动这个:

android:layout_toRightOf ="@id/first"

android:layout_toLeftOf ="@id/myImage"

在您的 EditText 中,而不是在您的 ImageView 中。

因为现在您的 ImageView 统治了场景,并且必须相应地放置 EditText(此外,您必须在 ImageView 之后声明 EditText,否则仍然没有分配 ImageView 的 id)。

假设现在必须将 EditText 放置在 ImageView“附近”(toLeftOf),在 ImageView 创建并放置之后。

换句话说,这就是你想要的:

<RelativeLayout
    android:background="#ff0000" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    >
    <ImageView
        android:src="@drawable/test_grid16x16" 
        android:id="@+id/myImage"
        android:layout_width="16dp" 
        android:layout_height="16dp"
        android:alignParentRight="true"
    />
    <EditText
        android:layout_width="200dp" 
        android:layout_height="wrap_content" 
        android:layout_toLeftOf ="@id/myImage"
        android:text="First line" 
        android:id="@+id/first"
    />
</RelativeLayout>

请注意,在这里和那里,您使用的是单引号 ('),而不是双引号 (")。

【讨论】:

  • 谢谢。我用你的例子使它工作。关键是你不能同时使用 android:layout_toRightOf 和 layout_alignParentRight。
  • 对不起!我的错字...我没有更正 EditText 中的 toLeftOf - 我确定我做到了 - 现在就这样做(我在描述中说过,然后忘记修改布局!)
【解决方案2】:

试试这个代码-

<RelativeLayout android:background="#ff0000" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<EditText android:layout_width="200dp" 
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/myimg"  
android:text="First line"
android:id="@+id/first" />

<ImageView android:id="@+id/myimg"
android:src="@drawable/test_grid16x16" 
android:layout_width="16dp" 
android:layout_height="16dp"
android:layout_alignParentRight="true" 
/>

</RelativeLayout>

希望这行得通。

或者在您的代码中添加 - android:layout_alignParentRight="true" in ImageView

还有android:layout_alignParentLeft="true"EditText

在 android 属性中使用 " 而不是 '

【讨论】:

  • 这行不通。除非您交换 ImageView 和 EditText 的定义。原因很简单,EditText 引用了 ImageView 的 id,而这仍未创建
  • 而这个:toLeftOf="@+id/myimg" 真的应该写成 toLeftOf="@id/myimg" (没有 plus )。加号只需要创建一个新的 id。
  • 这就是为什么不需要交换。
  • 顺便说一句,格式化代码被普遍认为是一种最佳实践...... ;)
猜你喜欢
  • 1970-01-01
  • 2018-06-25
  • 2019-04-10
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多