android linearlayout 下边框,如何在Android LinearLayout周围创建边框?

Sure. You can add a border to any layout you want. Basically, you need to create a custom drawable and add it as a background to your layout. example:

Create a file called customborder.xml in your drawable folder:

Now apply it as a background to your smaller layout:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/customborder">

That should do the trick.

Also see:

Creat XML called border.xml in drawable folder as below :

then add this to linear layout as backgound as this:

android:background="@drawable/border"

Create a one xml file in drawable folder

android:width="2dp"

android:color="#B40404" />

android:bottom="5dp"

android:left="5dp"

android:right="5dp"

android:top="5dp" />

Now call this xml to your small layout background

android:background="@drawable/yourxml"

Try this:

For example, let's define res/drawable/my_custom_background.xml as:

(create this layout in your drawable folder) layout_border.xml

android:color="#FF0000" />

android:bottom="1dp" />

android:bottomLeftRadius="0dp" android:topLeftRadius="5dp"

android:topRightRadius="0dp" />

main.xml

android:layout_gravity="center"

android:layout_width="200dp"

android:layout_height="200dp"

android:background="@drawable/layout_border" />

This solution will only add the border, the body of the LinearLayout will be transparent.

First, Create this border drawable in the drawable folder, border.xml

android:shape="rectangle">

Then, in your LinearLayout View, add the border.xml as the background like this

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="@drawable/border">

you can do it Pragmatically also

GradientDrawable gradientDrawable=new GradientDrawable();

gradientDrawable.setStroke(4,getResources().getColor(R.color.line_Input));

Then set the background of layout as :

LinearLayout layout = (LinearLayout ) findViewById(R.id.ayout); layout .setBackground(gradientDrawable);

I'll add Android docs link to other answers.

It describes all attributes of the Shape Drawable and stroke among them to set the border.

Example:

Red border with transparent background.

Don't want to create a drawable resource?

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@android:color/black"

android:minHeight="128dp">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_margin="1dp"

android:background="@android:color/white">

It's not exactly an answer for the question but for them wich just want to display the border of elements for debugging you could just go in your devloppers settings and turn on the show layout bounds in the drawing section

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值