2014/06/07

[android]style.xmlを使ったactivityの最適化

初めてのAndroid第三版を読むと下のような記述が随所にある。

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent
    android:id="@+id/hoge_button"
    android:text="@string/hoge"
/>

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent
    android:id="@+id/foo_button"
    android:text="@string/foo_label"
/>

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent
    android:id="@+id/bar_button"
    android:text="@string/bar_label"
/>
上の書き方なんだけど、Styles and Themesを読むと、以下のようにすることができそうだ。
<-- activity_fullscreen.xml -->
<Button
    style="@style/BaseViewTheme"
    android:id="@+id/hoge_button"
    android:text="@string/hoge"
/>

<Button
    style="@style/BaseViewTheme"
    android:id="@+id/foo_button"
    android:text="@string/foo_label"
/>

<Button
    style="@style/BaseViewTheme"
    android:id="@+id/bar_button"
    android:text="@string/bar_label"
/>
<-- res/values/styles.xml -->
<style name="BaseViewTheme">
  <item name="android:layout_width">match_parent</item>
  <item name="android:layout_height">wrap_content</item>
</style>
これ確かに動くといえば動くのだが、なぜ「android:」という文字が必要なのだろうか?

androidで使うことが前提なので、短絡的に考えて
<-- res/values/styles.xml -->
<style name="BaseViewTheme">
  <item name="layout_width">match_parent</item>
  <item name="layout_height">wrap_content</item>
</style>
でもいいと思うのだが。
初めてのAndroid 第3版
初めてのAndroid 第3版
posted with amazlet at 14.06.09
Ed Burnette
オライリージャパン
売り上げランキング: 348,673

0 コメント:

コメントを投稿