android add layout,Add a Border to an Android Layout

本文介绍了如何在Android应用中使用XML形状资源为布局添加边框,包括设置矩形形状、圆角、内边距和颜色。通过修改属性值,开发者可以自定义界面的外观,提升用户体验。示例代码展示了如何创建一个带有自定义边框的布局,并将其应用于TextView,同时提供了下载代码和进一步实验的建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://tekeye.biz/2012/add-a-border-to-an-android-layout

可参考:http://stackoverflow.com/questions/1598119/is-there-an-easy-way-to-add-a-border-to-the-top-and-bottom-of-an-android-view

The interface to an Android Application must be easy to use, intuitive and pleasing to the eye. The Android SDK makes it easy to generate screens, but the developer should add a little extra to make the interface attractive. Fortunately Android also makes it easy to enhance an interface because layouts and views have attributes that affect the appearence when a screen is displayed. One of the most versatile is the background attribute, it can be set to colors, images, nine patch files and shapes. In this article a border is placed around an area of the screen to add interest to the interface.

0818b9ca8b590ca3270a3433284dd417.pngDevelopers first come across the res/drawable folder when dealing with the Apps launcher icon, as well as other image files (PNG or JPG). The folder can also hold other graphical resources, including XML definitions of shapes, colours and animations. Any drawable can be referenced from any screen. In this simple example project a shape is used to add a rectangular border with rounded corners to a layout file.

Create a new project in Eclipse (if new to programming see Your First Android Java Program), give it a name, for example Layout Border. Use the Eclipse Package Explorer to highlight the res/drawable folder. Use the context menu or the Filemenu and select New then Android XML File. In the New Android XML File dialog the Resource Type is set to Drawable and Project is set to Layout Border (or the name you gave it). Enter a file name, for example customborder.xml, then select shape as the root element.

0818b9ca8b590ca3270a3433284dd417.png

Click Finish, and in the file that is created the code is added to define a rectangle that has rounded corners, some padding and a solid color. The final code will be explained below but looks like this (see Copying Code from the Articles for copying tips) :

[code lang=”xml”]<?xml version="1.0" encoding="UTF-8"?>

[/code]

On the root shapeelement the attribute android:shape is set to rectangle (shape files also supportoval, line and ring). Rectangle is the default value so this attribute could be left out if a rectangle is being defined. See the Android documentation on shapes for detailed information on a shape file.

The element corners sets the rectangle corners to be rounded, it is possible to set a different radius on each corner (see the Android reference).

The padding attributes are used to move the contents of the View to which the shape is applied, to prevent the contents overlapping the border.

The border color here is set to a light gray (CCCCCC hexadecimal RGB value).

Shapes also support gradients but that is not being used here, again see the Android resources to see how a gradient is defined. To use the shape reference the drawable from a layout or view using theandroid:background attribute, in this case it would be used with android:background="@drawable/customborder".

Here it is applied to a layout, to which other views can be added as normal. In this example a singleTextView has been added, the text is white (FFFFFF hexadecimal RGB). The TextView’s background is set to blue, plus some transparency to reduce the brightness (A00000FF hexadecimal alpha RGB value). Finally the layout is offset from the screen edge by placing it into another layout with a small amount of padding. Replace the existing layout code in the main.xml file in res/layout with this:

[code lang=”xml”]<?xml version="1.0" encoding="utf-8"?>

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="5dp">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/customborder">

android:layout_height="fill_parent"

android:text="Text View"

android:textSize="20dp"

android:textColor="#FFFFFF"

android:gravity="center_horizontal"

android:background="#A00000FF" />

[/code]

Run the App and the result will be similar to this:

0818b9ca8b590ca3270a3433284dd417.png

Play around with values and attributes to see what effect they have on the layout, use the Android documentation to understand what the attributes do. Try adding other layouts and controls to experiment with different looks. Getting an App to look good adds to its appeal.

Download the code for this article ready for importing into an Android project. The code can also be accessed via the Android Example Projects page. See the article Move Android Code Between PCs Running Eclipse on how to import example code into an Eclipse project. A version of this article appears in the Android Cookbook.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值