<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:padding="5dp">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@id/imageView1" <!-- error -->
android:layout_alignParentLeft="true"
android:layout_alignTop="@id/imageView1" <!-- error -->
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/checkBox1"
android:src="@drawable/folder" />
我希望ImageView位于复选框的右侧,但我还希望checkBox为&#34; high&#34;作为ImageView(复选框应该仍然是正方形)
问候
答案 0 :(得分:3)
@kalyan pvs ....没关系,但你不应该每次都用“@ + id”调用,这意味着你每次都要创建新的实例。这是在相对布局中引用项目的错误方式。在这种情况下,当您引用将在当前视图之后创建的视图时,将出现渲染错误。调用引用的正确方法是@id only。
答案 1 :(得分:1)
只需交换CheckBox和ImageView并从ImageView中删除android:layout_toRightOf="@+id/checkBox1"
(以避免circular reference
):
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/folder"
/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@id/imageView1"
android:layout_alignParentLeft="true"
android:layout_alignTop="@id/imageView1"
android:layout_centerVertical="true"
/>
否则,CheckBox无法引用ImageView的id,因为它尚未创建
<强> [编辑] 强>
更好的方法是将ImageView合并到CheckBox中,作为复合drawable:
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:drawableRight="@drawable/folder"
android:layout_drawablePadding="10dp"
/>
请注意,现在图片位于复选框的右侧 这是最佳做法。
答案 2 :(得分:0)
稍后声明View
不是问题,请尝试此操作,减少代码的某些行。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:padding="5dp" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkBox1"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
答案 3 :(得分:0)
这样的事情对我有用:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:padding="5dp">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageView1"
android:layout_centerVertical="true" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkBox1"
android:src="@drawable/folder" />
</RelativeLayout>