16

如何以编程方式向 LinearLayout 添加边框?假设我们创建了这个布局:

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

那我该怎么办?

4

4 回答 4

70

我相信上面的答案是不正确的:该问题专门要求程序化版本来执行此操作,而您看到的第一件事是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);
    }
于 2015-04-02T14:06:56.487 回答
5

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

然后将其添加到线性布局中,如下所示:

android:background="@drawable/border"

以编程方式

TitleLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.border))

编辑 :

自从果冻豆,这个方法(setBackgroundDrawable 已被弃用),所以你必须使用这个:

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

希望这有帮助。

于 2013-06-02T16:40:12.610 回答
1

对于 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));
于 2018-07-20T07:33:04.803 回答
1

您可以使用描边创建渐变可绘制对象。您可以通过编程方式修改笔触宽度和颜色。

private fun createGradientBackground(@ColorInt startColor: Int, @ColorInt endColor: Int) =
    GradientDrawable(
        GradientDrawable.Orientation.LEFT_RIGHT, intArrayOf(
            startColor,
            endColor
        )
    ).also {
        it.shape = GradientDrawable.RECTANGLE
        it.cornerRadius = GRADIENT_CORNER_RADIUS
        it.setStroke(YOUR_WIDTH, YOUR_COLOR)
    }
于 2021-08-26T07:54:02.260 回答