以编程方式设置前台资源

问题描述 投票:0回答:1

我创建了这样的LinearLayout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:id="@+id/LayoutProfit"
    android:layout_width="0dp"
    android:layout_weight="20"
    android:layout_height="27dp"
    android:minWidth="0px"
    android:minHeight="50px"
    android:background="#edf0f4"
    android:foreground="@drawable/list_divider_full">

现在,我想以编程方式更改前台资源,但是我不知道如何。我可以这样更改背景资源:

LayoutProfit.SetBackgroundResource(Resource.Drawable.list_divider_top_sides);

我想更改布局的颜色和边框,但是如果我同时使用背景,那是行不通的,因为它是边框或颜色,然后...

那么,如何更改前景资源?

c# android xamarin.android foreground
1个回答
0
投票

如果要设置布局的前景色,可以使用:

var LayoutProfit = FindViewById<LinearLayout>(Resource.Id.LayoutProfit);
var drawable = new GradientDrawable();
drawable.SetColor(Resource.Color.colorAccent);
LayoutProfit.Foreground = drawable;

但是,如果要在此布局上设置边框,则必须在Resources/drawable/border.xml中将形状定义为:

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <stroke android:width="5dip" android:color="@android:color/holo_red_dark" />
</shape>

然后将其用于您的布局,例如:

var LayoutProfit = FindViewById<LinearLayout>(Resource.Id.LayoutProfit);
LayoutProfit.SetBackgroundResource(Resource.Drawable.border);
© www.soinside.com 2019 - 2024. All rights reserved.