Android用户界面:大小2视图?
大家好,目前我正在尝试制作我的第一个Android应用程序,然后是我意识到的第一件事,愚蠢的XML UI设计 我有这个2视图(按钮),我想让它们这样第一个填充父对象的一半(RelativeLayout),第二个填充父对象的另一半。。。 我的代码是:Android用户界面:大小2视图?,android,xml,user-interface,Android,Xml,User Interface,大家好,目前我正在尝试制作我的第一个Android应用程序,然后是我意识到的第一件事,愚蠢的XML UI设计 我有这个2视图(按钮),我想让它们这样第一个填充父对象的一半(RelativeLayout),第二个填充父对象的另一半。。。 我的代码是: <Button android:id="@+id/Top1" android:layout_width="wrap_content" android:layout_height="matc
<Button
android:id="@+id/Top1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/Top2"
android:text="top1"/>
<Button
android:id="@+id/Top2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/Top1"
android:text="TOP2"/>
问题是,我得到了这个错误:
“未找到与给定名称匹配的资源(位于'layout_toLeftOf'处,值为'@id/Top2')。”
似乎连Android都不想使用XML。
就像一个旧的C程序,如果在调用下面写一个方法,它会给出一个错误
所以我有两个问题:
1:如何在XML中解决这个问题?
2:或者我可以避免这种XML设计,使用类似于C#?中的代码设计吗?如果你是正确的,
Top2
需要先在XML中定义,然后才能引用它。这就是说,如果你所做的是把2个按钮彼此相邻,并让它们填充父代,你应该考虑使用<代码>线性布局< /代码>。顺序在那里也很重要:对于水平方向,孩子们从左到右排列,对于垂直方向,他们从上到下排列。有几种方法可以做到这一点,我提供了两种方法让按钮并排排列:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/btn_container">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Left Button"
android:layout_weight="1"
android:id="@+id/btn_left" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Right Button"
android:id="@+id/btn_right" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/btn_left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@id/strut"
android:text="Left Button" />
<Button
android:id="@+id/btn_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/strut"
android:layout_alignParentRight="true"
android:text="Right Button" />
</RelativeLayout>
苏。。。有没有一种方法,我在XML中创建一个“声明字段”,然后进行组织?在帕斯卡语中,先声明这些东西,然后再使用它们?谢谢。。。这将解决第一个问题。。。在这个垃圾XML设计中,第二个更难…只是需要一些时间来习惯使用XML进行设计。或者,你可以用代码来完成上面的所有工作,但我不喜欢用代码来做设计工作。我更喜欢代码设计方式,我认为它给了我更多的优势。。。主要是“编译器”类型,而不是“解释器”,比如……你试过Android Studio的框架吗?在大多数情况下,您甚至不需要接触XML代码。只需拖放和调整大小。