Android 以编程方式将边框添加到LinearLayout

Android 以编程方式将边框添加到LinearLayout,android,layout,border,Android,Layout,Border,如何以编程方式向线性布局添加边框? 假设我们创建此布局: LinearLayout TitleLayout = new LinearLayout(getApplicationContext()); TitleLayout.setOrientation(LinearLayout.HORIZONTAL); 那我该怎么办?在drawable文件夹中创建名为border.XML的XML,如下所示: <?xml version="1.0" encoding="utf-8"?> <l

如何以编程方式向线性布局添加边框? 假设我们创建此布局:

LinearLayout TitleLayout = new LinearLayout(getApplicationContext());
TitleLayout.setOrientation(LinearLayout.HORIZONTAL);

那我该怎么办?

在drawable文件夹中创建名为border.XML的XML,如下所示:

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item> 
    <shape android:shape="rectangle">
      <solid android:color="#FF0000" /> 
    </shape>
  </item>   
    <item android:left="5dp" android:right="5dp"  android:top="5dp" >  
     <shape android:shape="rectangle"> 
      <solid android:color="#000000" />
    </shape>
   </item>    
 </layer-list> 
以编程方式

TitleLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.border))
编辑:

自Jelly Bean以来,此方法(setBackgroundDrawable已被弃用),因此您必须使用此方法:

TitleLayout.setBackground(getResources().getDrawable(R.drawable.border));

希望这能有所帮助。

我相信上面的答案是不正确的:这个问题专门要求一个编程版的来完成,您看到的第一件事就是xml。 其次,在我的例子中,部分使用xml几乎从来都不是一个选项,因此下面是正确的答案:

    //use a GradientDrawable with only one color set, to make it a solid color
    GradientDrawable border = new GradientDrawable();
    border.setColor(0xFFFFFFFF); //white background
    border.setStroke(1, 0xFF000000); //black border with full opacity
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
      TitleLayout.setBackgroundDrawable(border);
    } else {
      TitleLayout.setBackground(border);
    }
//使用只有一个颜色集的GradientDrawable,使其成为纯色
GradientDrawable边框=新的GradientDrawable();
border.setColor(0xFFFFFFFF)//白色背景
设置行程(1,0xFF000000)//完全不透明的黑色边框
if(Build.VERSION.SDK\u INT
对于Xamarin用户:

添加新的类边框:

public class Border : Android.Graphics.Drawables.Drawable
{
    public Android.Graphics.Paint paint;
    public Android.Graphics.Rect bounds_rect;

    public Border(Android.Graphics.Color colour, int width)
    {
        this.paint = new Android.Graphics.Paint();
        this.paint.Color = colour;
        this.paint.StrokeWidth = width;
        this.paint.SetStyle(Android.Graphics.Paint.Style.Stroke);
    }

    public override int Opacity => 0;
    protected override void OnBoundsChange(Rect bounds)
    {
        base.OnBoundsChange(bounds);
        this.bounds_rect = bounds;
    }

    public override void Draw(Canvas canvas)
    {
        canvas.DrawRect(this.bounds_rect, this.paint);
    }

    public override void SetAlpha(int alpha)
    {
        //throw new NotImplementedException();
    }

    public override void SetColorFilter(ColorFilter colorFilter)
    {
        //throw new NotImplementedException();
    }
}
然后像这样使用它:

TitleLayout.SetBackgroundDrawable(new Border(Color.Black, 5));

我试过了。它不起作用。eclipse还说setBackgroundDrawable()已被弃用。它可以工作,eclipse只是说它已被弃用,因为它在Jelly Bean之后还存在另一个方法:TitleLayout.setBackground(getResources().getDrawable(R.drawable.border));我使用的是“btn_tod.setBackgroundDrawable(new ColorDrawable(R.drawable.nobottom));”,MDMalik的解决方案有着奇怪的效果,“btn_tod.setBackgroundDrawable(getResources().getDrawable(R.drawable.nobottom));”。我不明白为什么“新的彩色绘图”不起作用而“getDrawable”起作用。“new ColorDrawable”似乎适用于自定义颜色,但不适用于XML Drawable。getDrawable(int)现在也不推荐使用。现在需要添加一个主题:getDrawable(int,Resources.Theme)如何绘制两种颜色的渐变?适用于线性布局,但不适用于视图。这是为什么?我如何修复它?当对视图对象使用它时,我发现行
border.setColor(0xFFFFFFFF)//白色背景
似乎覆盖了视图中的内容。在这种情况下,删除这一行并仅使用
border.setStroke(…)
对我很有效。
TitleLayout.SetBackgroundDrawable(new Border(Color.Black, 5));