Android 与边框相对的Allign视图
有没有办法在边框旁边的relativelayout中布局视图 我有下面的布局(简化的测试用例),我希望将图像移动到尽可能远的右边,使其与右边框对齐。有什么方法可以做到这一点吗Android 与边框相对的Allign视图,android,layout,view,android-relativelayout,Android,Layout,View,Android Relativelayout,有没有办法在边框旁边的relativelayout中布局视图 我有下面的布局(简化的测试用例),我希望将图像移动到尽可能远的右边,使其与右边框对齐。有什么方法可以做到这一点吗 <RelativeLayout android:background="#ff0000" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:layout_width='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: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>
在RelativeLayout中,childen可以将这些属性设置为true以与其容器的边界对齐:alignParentTop、alignParentBottom、alignParentLeft和alignParentRight;并且可以组合 在您的情况下:将alignParentRight=“true”添加到ImageView 我还要提出以下建议:
android:layout_toRightOf ="@id/first"
到
在编辑文本中,而不是在图像视图中
因为现在您的ImageView控制场景,并且EditText必须相应地放置(另外,您必须在ImageView之后声明EditText,否则ImageView的id仍然没有指定)
假设在创建并放置ImageView之后,现在必须将EditText放置在ImageView的“附近”(toLeftOf)
换句话说,这就是你想要的:
<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>
请注意,在这里和那里,您放置了一个单引号('),而不是双引号(“)。试试下面的代码-
<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>
希望这能奏效
或者在您的代码中,在ImageView
中添加-android:layout\u alignParentRight=“true”
和EditText
中的android:layout\u alignParentLeft=“true”
在android属性中使用“而不是”填充父项不受欢迎,请使用匹配父项。填充父项不受欢迎。它不起作用。除非您交换ImageView和EditText的定义。原因很简单,EditText引用的是ImageView的id,但该id尚未创建。而这个:toLeftOf=“@+id/myimg”实际上应该写入toLeftOf=“@id/myimg”(不带加号)。加号只需要创建一个新id。这就是为什么不需要交换。顺便说一句,格式化代码通常被认为是最佳实践…;)谢谢我用你的例子使它起作用。关键是你不能同时使用android:layout\u toRightOf和layout\u alignParentRight。对不起!我的打字错误。。。我没有纠正EditText中的toLeftOf,我确信我已经纠正了,现在就做(我在描述中说了,然后忘了修改布局!!)
<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>