How to use dens.xml in android?

When I create the layout, I centralize all dimensions in the dimensions.xml due to maintainability. My question is whether this is correct or not. What would be the best good practice? There is little information about this, nothing. I know that it is a good idea to centralize all layout lines on strings.xml, colors on colors.xml. But about the sizes?

For instance:

<TableLayout
    android:id="@+id/history_detail_rows_submitted"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/cebroker_history_detail_rows_border"
    android:collapseColumns="*">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/history_detail_rows_margin_vertical"
        android:background="@color/cebroker_history_detail_rows_background"
        android:gravity="center"
        android:paddingBottom="@dimen/history_detail_rows_padding_vertical"
        android:paddingLeft="@dimen/history_detail_rows_padding_horizontal"
        android:paddingRight="@dimen/history_detail_rows_padding_horizontal"
        android:paddingTop="@dimen/history_detail_rows_padding_vertical">

        <TextView
            android:layout_width="match_parent"
            android:drawableLeft="@mipmap/ic_history_detail_submitted_by"
            android:drawablePadding="@dimen/history_detail_rows_textviews_padding_drawable"
            android:gravity="left|center"
            android:paddingRight="@dimen/history_detail_rows_textviews_padding"
            android:text="@string/history_detail_textview_submitted_by"
            android:textColor="@color/cebroker_history_detail_rows_textviews"
            android:textSize="@dimen/history_detail_rows_textviews_text_size" />
+4
source share
5 answers

How to use dimens.xml

  • dimens.xml , values . dimens . ( dimen dimensions. , dimen, .)

  • dimen.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <dimen name="my_value">16dp</dimen>
    </resources>
    

    dp, px sp.

  • xml

    <TextView
        android:padding="@dimen/my_value"
        ... />
    

    float sizeInPixels = getResources().getDimension(R.dimen.my_value);
    

dimens.xml

.

  • . (, TextView textSize), dimen . , .

  • . 8dp , 10- . dimens.xml , - set 8dp 64dp . dimens.xml, res > . ( . )

  • dp to px . . , . dp, , float:

    float sizeInPixels = getResources().getDimension(R.dimen.my_value);
    

    int:

    int sizeInPixels = getResources().getDimensionPixelSize(R.dimen.my_value);
    

, .

dimens.xml

  • dimens.xml, . , , , . dimens.xml , , , . ( ) .

  • . , strings.xml, . , . Android Studio, , . xml , dp .

+7

xml dimens.xml, .

 <resources>
 <!-- Default screen margins, per the Android Design guidelines. -->
  <dimen name="iconarrow">1dp</dimen>
  <item name="text_view_padding" type="integer">100</item>
</resources>

, java-

textview.setPadding(0, 0, 0, getResources().getInteger(R.integer.text_view_padding));

( xml).

android:padding="@dimen/text_view_padding"
+1
But about dimensions?

Android " - , , , name ( XML). , XML , <resources>"

. http://developer.android.com/guide/topics/resources/more-resources.html

Devunwired , dimens.xml dimens.xml Android?

0

@Jesús Castro . dimens.xml , .

, , . , dimens.xml, - . , , "16dp" ( , , "@dimen/leftright_margin" ), , .

0

. ,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/_20sdp"
android:paddingLeft="@dimen/_20sdp"
android:paddingRight="@dimen/_20sdp"
android:paddingTop="@dimen/_20sdp"
android:background="@color/colorPrimary"
android:orientation="vertical"
 >

0

All Articles