Codejie's C++ Space

Using C++

Android DEV : Amazing Theme and Style


    工作上需要动态改变app字体大小,而我的LAC也可能需要这个,于是半公半私地看起来...google了很多页面,越发觉得Theme和Style神奇了...

    咱也讲不好,还是直接放上几段代码做记录吧...

    styles.xml

<resources>
    <style name="small_title_text">
      <item name="android:textSize">22sp</item>
      <item name="android:textColor">@color/color1</item>
      <item name="android:textStyle">normal</item>
      <item name="android:paddingBottom">5dip</item>
   </style>
   <style name="small_body_text">
      <item name="android:textSize">16sp</item>
      <item name="android:textColor">@color/color1</item>
      <item name="android:textStyle">normal</item>
      <item name="android:paddingBottom">5dip</item>
   </style>
   <style name="large_title_text">
      <item name="android:textSize">40sp</item>
      <item name="android:textColor">@color/color</item>
      <item name="android:textStyle">normal</item>
      <item name="android:paddingBottom">5dip</item>
   </style>

   <style name="large_body_text">
      <item name="android:textSize">20sp</item>
      <item name="android:textColor">@color/color</item>
      <item name="android:textStyle">normal</item>
      <item name="android:paddingBottom">5dip</item>
   </style>

  <!-- Base application theme is the default theme. -->
  <style name="Theme" parent="android:Theme">
  </style>

  <style name="Theme.Small" parent="Theme">
     <item name="textTitle">@style/small_title_text</item>
     <item name="textBody">@style/small_body_text</item>
  </style>

  <style name="Theme.Large"  parent="Theme">
      <item name="textTitle">@style/large_title_text</item>
      <item name="textBody">@style/large_body_text</item>
  </style>
    
</resources>

    attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
       <attr name="textTitle" format="reference" />
       <attr name="textBody" format="reference" />    
</resources>

    main.xml
<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/textView1"
        style
="?textTitle"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:layout_alignParentLeft
="true"
        android:layout_alignParentTop
="true"
        android:text
="@string/hello_world" />

    <Button
        
android:id="@+id/button1"
        style
="?textBody"
        android:layout_width
="match_parent"
        android:layout_height
="wrap_content"
        android:layout_alignLeft
="@+id/textView1"
        android:layout_below
="@+id/textView1"
        android:layout_marginTop
="14dp"
        android:text
="ToggleButton" />

</RelativeLayout>

    main.java
    protected void onButtonClick() {
        
        if (toggle) {
            this.setTheme(R.style.Theme_Large);
            setContentView(R.layout.activity_main);
        } else {
            this.setTheme(R.style.Theme_Small);
            setContentView(R.layout.activity_main);            
        }
        Button btn = (Button) this.findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                onButtonClick();
            }
            
        });
        
        toggle = !toggle;
    }


file://C:\Program Files\sdk\platforms\android-17\data\res
http://developer.android.com/guide/topics/ui/themes.html
http://stackoverflow.com/questions/3241729/android-dynamically-change-style-at-runtime

posted on 2013-05-10 14:37 codejie 阅读(397) 评论(2)  编辑 收藏 引用 所属分类: 随笔而已G7

评论

# re: Android DEV : Amazing Theme and Style 2013-05-14 19:15 jan

看着好亲切啊,咱这些天,就是被这些东西.欺负的  回复  更多评论   

# re: Android DEV : Amazing Theme and Style[未登录] 2013-05-14 23:13 codejie

@jan
我敢说这几天欺负你的一定不是它们...表打错了哦...它们兄弟们间只是长的差不多而已了,就跟包子和馒头一样...  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

Using C++

导航

统计

留言簿(73)

随笔分类(513)

积分与排名

最新评论

阅读排行榜

评论排行榜