【发布时间】:2014-08-25 19:47:15
【问题描述】:
我正在尝试在 Xamarin 中的 Android 应用程序中向 RelativeLayout 的搜索表单区域添加背景颜色。基本上我有一个 EditText 和一个按钮,应该包含在屏幕顶部的彩色背景中。使用下面的代码,应用了背景颜色,但我的 EditText 和按钮消失了,而它们下面的内容向上移动并取而代之。这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:screenOrientation="portrait"
android:background="#ffffff">
<RelativeLayout
android:id="@+id/TestLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EAF4F9">
<com.neopixl.pixlui.components.textview.TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Heading"
pixlui:typeface="Lato-Bold.ttf"
android:layout_marginBottom="15dp"/>
<EditText
android:inputType="numberDecimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:id="@+id/SearchField"
android:layout_marginRight="10dp"/>
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:layout_toRightOf="@id/SearchField"
android:id="@+id/btnSearch"
android:layout_alignParentRight="true" />
<TextView
android:text="Use my current location"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="@id/SearchField"
android:id="@+id/txtCurrentLocation"
android:gravity="center"/>
</RelativeLayout>
<View
android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<com.neopixl.pixlui.components.button.Button
android:id="@+id/btnOne"
android:text="Button One"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_alignRight="@id/strut"
android:layout_alignParentLeft="true"
android:layout_below="@id/txtCurrentLocation"
pixlui:typeface="Lato-Light.ttf"
android:gravity="center" />
<com.neopixl.pixlui.components.button.Button
android:text="Button Two"
android:id="@+id/btnTwo"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_alignLeft="@id/strut"
android:layout_alignParentRight="true"
android:layout_below="@id/txtCurrentLocation"
android:gravity="center"
pixlui:typeface="Lato-Light.ttf" />
</RelativeLayout>
我应该在这里使用 LinearLayout 还是有更好的方法来构造它?
【问题讨论】:
-
为什么嵌套的
RelativeLayout中的两个元素都使用android:layout_below="@+id/textView1"?请张贴整个布局。RelativeLayout几乎总是不需要完全嵌套,这太酷了! -
我已经编辑了上面的代码以显示完整的布局。基本上,您在顶部有一个标题,左边有一个搜索字段,右边有一个搜索按钮。搜索字段和按钮下方是使用当前位置的按钮。所有这些都应该在跨越父级宽度的浅蓝色背景上。如果不需要将其包装在某种容器中,我还能如何实现这一目标?谢谢!
标签: android android-layout xamarin xamarin.android android-relativelayout