【问题标题】:Xamarin - Set a background color and a border for a textviewXamarin - 为文本视图设置背景颜色和边框
【发布时间】:2018-12-22 08:58:37
【问题描述】:

我想有一个非常简单的问题......

我有一个 textview,我希望 TextView 有一个边框,所以我这样做了:

<TextView
    android:text="Status"
    android:layout_width="wrap_content"
    android:layout_weight="16"
    android:layout_height="150px"
    android:id="@+id/txtStatus"
    android:textColor="#000000"
    android:textSize="50px"
    android:paddingTop="20px"
    android:paddingLeft="3dip"
    android:background="@drawable/list_divider"/>

list_divider.xml 包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
  <stroke
      android:width="1dp"
      android:color="#FF000000" />
</shape>

此代码有效,但在 c# 类中,我使用以下命令设置背景颜色: txtStatus.SetBackgroundColor(Color.ParseColor("#FF746B"));

但是当我使用该命令时,它会覆盖边框。我怎样才能同时拥有边框和背景颜色。我无法在 list_divider.xml 中分配它们,因为颜色取决于 TextView 的值。

所以要明确一点,我怎样才能在 textview 上同时拥有边框和背景颜色?

【问题讨论】:

    标签: c# android xamarin xamarin.forms


    【解决方案1】:

    您可以尝试将边框设置为前景。

    <TextView
        ...
        android:foreground="@drawable/list_divider"/>
    

    可以同时使用背景和前景。

    <TextView
        ...
        android:foreground="@drawable/border"
        android:background="@color/colorPrimary"/>
    

    您也可以像您的情况一样以编程方式更改背景颜色。

    【讨论】:

    • 边界消失?!
    • @Vreesie 不,您可以同时使用背景和前景。我改变了答案。
    • 我也可以通过编程方式更改前台吗?
    • @Vreesie 是的,当然。
    猜你喜欢
    • 2013-03-01
    • 1970-01-01
    • 2011-08-23
    • 2013-07-29
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    相关资源
    最近更新 更多