【问题标题】:Setting background color of RelativeLayout in Xamarin.Android在 Xamarin.Android 中设置 RelativeLayout 的背景颜色
【发布时间】: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


【解决方案1】:

试试这样的:

<?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 版本即可。

【讨论】:

  • 谢谢,成功了!没有意识到它就像使用视图和定义布局对齐一样简单。
  • 像这样的东西在 Android L 中甚至更好。AXML 的强大功能:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-25
  • 2012-10-16
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多