在Xamarin.Android中设置RelativeLayout的背景颜色

时间:2014-08-25 19:47:15

标签: android android-layout xamarin xamarin.android android-relativelayout

我正在尝试在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还是有更好的方法来构建它?

1 个答案:

答案 0 :(得分:3)

尝试这样的事情:

<?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:background="#ffffff">
    <View
        android:background="#EAF4F9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/header"
        android:layout_alignBottom="@+id/btnOne" />
    <TextView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="15dp"
        android:layout_alignParentTop="true"
        android:text="Hello" />
    <EditText
        android:inputType="numberDecimal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/header"
        android:layout_alignParentLeft="true"
        android:id="@+id/search"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:hint="Search for..." />
    <Button
        android:text="Search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnSearch"
        android:layout_marginRight="10dp"
        android:layout_alignParentRight="true"
        android:layout_alignBaseline="@+id/search" />
    <TextView
        android:text="Use my current location"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/search"
        android:layout_marginTop="10dp"
        android:id="@+id/txtCurrentLocation"
        android:gravity="center" />
    <Button
        android:id="@+id/btnOne"
        android:text="Button One"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_alignRight="@id/strut"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/txtCurrentLocation"
        android:gravity="center" />
</RelativeLayout>

只需在需要的地方用pixlui版本替换按钮和文本视图。

相关问题