設定當前視圖中所有控制項字型的方法

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   strong   檔案   

    

  本範例實現的是對介面中所有的控制項一次性的設定字型樣式。思路是找到父控制項,然後遍曆子控制項。如果子控制項是可以修改文字的控制項,那麼就設定文字。這用到了控制項的繼承,很多控制項都是繼承與textview的,所以將控制項均轉為textview,最後設定字型即可。

布局檔案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="113dp"        android:text="Small Text"        android:textAppearance="?android:attr/textAppearanceSmall" />    <EditText        android:id="@+id/editText1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/textView1"        android:layout_centerHorizontal="true"        android:layout_marginTop="41dp"        android:hint="kale"        android:gravity="center_horizontal"        android:ems="10" >        <requestFocus />    </EditText>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/editText1"        android:layout_marginTop="50dp"        android:text="Button" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_alignRight="@+id/textView1"        android:text="設定文字大小"        android:textAppearance="?android:attr/textAppearanceSmall" />    <SeekBar        android:id="@+id/seekBar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/textView2"        android:layout_centerHorizontal="true"        android:layout_marginTop="27dp"        android:max="40"        android:progress="16" /></RelativeLayout>

 

MainActivity

package com.kale.font;import android.app.Activity;import android.graphics.Typeface;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;public class MainActivity extends Activity {    private static int FLAG = 0;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                // Sets fonts for all        final Typeface mFont = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");        final ViewGroup root = (ViewGroup) findViewById(R.id.layout);        setFont(root, mFont,16);        FLAG = 1;//初始化完成後標誌位為1,滑動滑塊只修改字型大小。不重複指定字型了。                ((SeekBar) findViewById(R.id.seekBar)).setOnSeekBarChangeListener(new OnSeekBarChangeListener() {                        @Override            public void onStopTrackingTouch(SeekBar seekBar) {                            }                        @Override            public void onStartTrackingTouch(SeekBar seekBar) {                            }                        @Override            public void onProgressChanged(SeekBar seekBar, int progress,                    boolean fromUser) {                 setFont(root, mFont,progress);            }        });    }        /*     * Sets the font on all TextViews in the ViewGroup. Searches recursively for     * all inner ViewGroups as well. Just add a check for any other views you     * want to set as well (EditText, etc.)     */    public void setFont(ViewGroup group, Typeface font ,int textSize) {        int count = group.getChildCount();        View v;        for (int i = 0; i < count; i++) {            v = group.getChildAt(i);            if (v instanceof TextView || v instanceof EditText || v instanceof Button) {                if (FLAG == 0) {                    ((TextView) v).setTypeface(font);                }                ((TextView)v).setTextSize(textSize);            } else if (v instanceof ViewGroup)                setFont((ViewGroup) v, font,textSize);        }    }}

 

源碼下載:http://download.csdn.net/detail/shark0017/7732339

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.