Android > resource - Å©±â ¸®¼Ò½º <dimen>
µî·ÏÀÏ : 2017-07-05 16:57
Á¶È¸¼ö : 53,110
Å©±â ¸®¼Ò½º´Â º¸Åë /res/values/dimens.xml ¿¡¼ °ü¸®ÇÑ´Ù.
dimens.xml ÆÄÀÏ¿¡ <dimen>¸¦ »õ·Î µî·ÏÇÑ ÈÄ, xml°ú java¿¡¼ È£ÃâÇÏ¿© º¸ÀÚ.
1. res(¸®¼Ò½º)¿¡ <dimen> µî·ÏÇϱâ
/res/values/dimens.xml ÆÄÀÏ¿¡ µî·ÏÇÑ´Ù.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="wide_size">40px</dimen>
</resources>
- "activity_horizontal_margin", "activity_vertical_margin"Àº default·Î ¼³Á¤µÇ¾î ÀÖ´Ù.
- nameÀ» "wide_size"·Î <dimen<À» µî·ÏÇÑ´Ù.
- Å©±â´Â 40px ·Î µî·ÏÇÑ´Ù.
2. layoutÀÇ xml¿¡¼ ¸®¼Ò½º <dimen> °¡Á®¿À±â
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sample1"
android:textSize="@dimen/wide_size" />
</RelativeLayout>
- id : TextView01·Î TextView µî·Ï
- ¹®ÀÚ¿Àº sample1 À̸§À¸·Î µî·ÏµÈ string ¸®¼Ò½º µî·Ï
- "wide_size"·Î µî·ÏµÈ <dimen> ¸®¼Ò½º¸¦ ¹®ÀÚ¿ Å©±â·Î ÁöÁ¤ ( android:textSize="@dimen/wide_size" )
½ÇÇàȸéÀ» º¸ÀÚ!
- ÅؽºÆ®¿¡ <dimen>ÀÇ °ª Å©±â 40px ÀÌ Àû¿ëµÇ¾ú´Ù.
3. Activity.java ¿¡¼ <dimen> ¸®¼Ò½º °¡Á®¿À±â
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getResources().getDimension() À¸·Î ¸®¼Ò½º¿¡¼ <dimen> °ª È£Ãâ
float wideSize = getResources().getDimension(R.dimen.wide_size);
// id : TextView01 ÀÎ TextView È£Ãâ
TextView textView01 = (TextView)findViewById(R.id.TextView01);
// textView01ÀÇ ¹®ÀÚ¿ Å©±â¸¦ <dimen>¿¡¼ ºÒ·¯¿Â Å©±â·Î ¼ÂÆÃ
textView01.setTextSize(wideSize);
}
}
- getResources().getDimension() À¸·Î ¸®¼Ò½º¿¡ µî·ÏÇÑ <dimen>À» °¡Á®¿Â´Ù.
- TextView¸¦ È£Ãâ ÇÑ ÈÄ, setTextSize() ·Î °¡Á®¿Â <dimen> Å©±â¸¦ Àû¿ëÇÑ´Ù.
½ÇÇàȸéÀ» º¸ÀÚ!
- TextView01¿¡ <dimen> °ª 40pxÀÌ Àû¿ëµÇ¾ú´Ù.