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

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

我正在尝试将背景颜色添加到 Xamarin 中 Android 应用程序中的相对布局的搜索表单区域。基本上我有一个 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 还是有更好的方法来构造它?

最佳答案

尝试这样的事情:

<?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>

只需在需要时将按钮和 TextView 替换为 pixlui 版本即可。

关于android - 在Xamarin.Android中设置RelativeLayout的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25493272/

相关文章:

java - 如何在 Gridview Android 中保持 CheckBox 持久性(处理复选框状态)?

multithreading - Android ProgressBar 停止更新

android: 当我从我的电脑运行时,谷歌地图 v2 不工作

android - 无法在 Xamarin Studio 中加载 Main.axml

xamarin - 如何在xamarin表单的工具栏中添加选择器

java.lang.NoClassDefFoundError : com/android/utils/ILogger into visual studio 2015 错误

android - 如何在android中实现 "Rich Edittext"

android - “通用图像加载器”丢失了我的 ImageView 的参数(大小和位置)

xml - 迁移到 androidx 后错误膨胀类 androidx.recyclerview.widget.RecyclerView

Android EditText 默认样式