Android 布局XML视图ID和R问题
我正在用XML创建一个布局,我注意到了这个非常奇怪的现象。这以前从未发生过,但最近我一直很紧张。我有点想放弃Eclipse,并因此找到一个更好的IDE 以下是我的布局:Android 布局XML视图ID和R问题,android,eclipse,android-layout,android-resources,r.java-file,Android,Eclipse,Android Layout,Android Resources,R.java File,我正在用XML创建一个布局,我注意到了这个非常奇怪的现象。这以前从未发生过,但最近我一直很紧张。我有点想放弃Eclipse,并因此找到一个更好的IDE 以下是我的布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
<?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="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/action_search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="kkm" >
</EditText>
<ListView
android:id="@+id/actions_list"
android:layout_width="90dp"
android:layout_height="match_parent"
android:layout_above="@id/cancel_action_selection"
android:layout_below="@id/action_search_bar" >
</ListView>
<TextView
android:id="@+id/action_preview_pane"
android:layout_width="230dp"
android:layout_height="match_parent"
android:layout_alignBottom="@id/actions_list"
android:layout_alignTop="@id/actions_list"
android:layout_toRightOf="@id/actions_list"
android:gravity="center_vertical|center_horizontal"
android:text="kk" />
<Button
android:id="@+id/cancel_action_selection"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:text="Cancel" />
<Button
android:id="@+id/define_new_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/cancel_action_selection"
android:text="NEW" />
<Button
android:id="@+id/select_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/define_new_action"
android:text="OK" />
</RelativeLayout>
以下是我努力实现的目标:
问题是,如果我试图将ListView设置在cancel按钮上方(通过上面的android:layout_),它会说找不到cancel按钮的id
现在,我注意到,如果我尝试在使用@+id创建布局之前为布局指定任何id,那么我将得到一个奇怪的错误。我的意思是:
在XML中,显然必须按顺序声明ID。奇怪,不是吗?
在定义ListView并将其layout_设置为上述属性之前,我必须首先用@+id方式声明cancel按钮的id
如果我尝试执行我在这里所做的操作,它会说它找不到cancel按钮的id
我尝试过清理项目、结束adb.exe、刷新等,但它所做的一切都会进一步破坏我的项目。之后甚至不生成R。我不想陷入一个又一个问题的循环,因为Eclipse不能正确处理这样的事情
有人能解决这个问题吗?还是更好的IDE?
RelativeLayout允许子视图指定它们相对于父视图或彼此的位置(由ID指定)。因此,您可以通过右边框对齐两个元素,或使一个元素位于另一个元素的下方,在屏幕中居中,左居中,等等
因此,您将相对于“取消”按钮放置listview。因此,您需要使用id定义按钮。相对于按钮定位listview
在为线性布局和相对布局发布的链接中有一个示例,请看一看
编辑:
如果资源文件中有错误,将不会生成R.java。因此,请确保资源文件中没有错误,然后清理并生成项目
澄清混乱。注意android:layout_over=“@+id/button1”
使用
查看上面链接中的属性id您需要先拥有“取消”按钮id,因为您将listview放置在相对于“取消”按钮的位置。@Raghunandan这就是它与RelativeLayouts的工作方式吗?我必须对线性布局做同样的事情吗?线性布局是不同的。对于相对布局,您将相对其他视图放置视图。因此,首先需要按钮的id,然后相对于按钮放置ListViewOHHHHHHHH。我懂了。谢谢你。你为什么不直接把你的答复作为答复发出去呢?我会选择的。(还有,很抱歉我的问题让我很沮丧。这一整天都让我发疯。)@TonyN.Tran您不需要在
列表视图
之前定义取消
按钮。Xml是从上到下解析的。因此,eclipse/android只需要一个id
<代码>id使用@+id
声明,并使用@id
引用。由于cancel
按钮的定义晚于listview
,因此您需要android:layout_over=“@+id/cancel_action_selection”
(注意那里的+
)。现在,当布局膨胀时,listview
将根据需要放置,即使稍后遇到cancel
按钮。因此您需要首先使用id定义按钮。将listview相对于按钮定位:
:只是让您知道,这不是必需的。@user2558882这是为什么。如果您使用的是相对布局,您将如何相对于按钮定位listview,或者建议相对于listview放置按钮?。我不是说按钮在xml中的位置。您可以先使用按钮,然后使用列表视图。我说的是button,首先感谢您做了必要的更正。我猜你在写的时候是指别的意思,你需要先定义一个id为的按钮
@user2558882谢谢你的评论,让我知道我猜这个词首先误导了别人。我改了。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OtherScreenActivity1" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:text="Button" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button1"
>
</ListView>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OtherScreenActivity1" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/button1"
android:layout_alignParentTop="true" >
</ListView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:text="Button" />
</RelativeLayout>
android:layout_above="@id/cancel_action_selection"
android:layout_above="@+id/cancel_action_selection"