Книга: Beginning Android

Dimensions

Dimensions

Dimensions are used in several places in Android to describe distances, such as a widget’s padding. While this book usually uses pixels (e.g., 10px for ten pixels), there are several different units of measurement available to you:

• in and mm for inches and millimeters, respectively, based on the actual size of the screen

• pt for points, which in publishing terms is 1/72nd of an inch (again, based on the actual physical size of the screen)

• dp and sp for device-independent pixels and scale-independent pixels — one pixel equals one dp for a 160dpi resolution screen, with the ratio scaling based on the actual screen pixel density (scale-independent pixels also take into account the user’s preferred font size)

To encode a dimension as a resource, add a dimen element, with a name attribute for your unique name for this resource, and a single child text element representing the value:

<resources>
 <dimen name="thin">10px</dimen>
 <dimen name="fat">1in</dimen>
</resources>

In a layout, you can reference dimensions as @dimen/..., where the ellipsis is a placeholder for your unique name for the resource (e.g., thin and fat from the previous sample). In Java, you reference dimension resources by the unique name prefixed with R.dimen. (e.g., Resources.getDimen(R.dimen.thin)).

Оглавление книги

Оглавление статьи/книги

Генерация: 1.640. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз