رفتن به مطلب
انجمن اندروید ایران | آموزش برنامه نویسی اندروید و موبایل
  • android.png.1fab383bc8500cd93127cebc65b1dcab.png

پست های پیشنهاد شده

سلام دوستان

از آموزش های همین سایت نحوه سفارشی کردن فونت رو برای یه برنامه خوندم ، ولی وقتی برنامه رو اجرا میکنم فورس کلوز میده

این از آموزش :

http://p30droid.com/Thread-%D8%A2%D9%85%D9%88%D8%B2%D8%B4-%D8%B4%D8%AE%D8%B5%DB%8C-%D8%B3%D8%A7%D8%B2%DB%8C-%D8%A8%D8%AE%D8%B4-%D8%AA%D9%86%D8%B8%DB%8C%D9%85%D8%A7%D8%AA-%D9%81%D9%88%D9%86%D8%AA-%D9%88-%D8%B3%D8%A7%DB%8C%D8%B2-%D9%88-%D8%B1%D9%86%DA%AF-%D9%88?highlight=%D8%B4%D8%AE%D8%B5%DB%8C+%D8%B3%D8%A7%D8%B2%DB%8C

اینم لاگ کت برنامه :

http://uupload.ir/files/6gb9_untitled.png

اینم کد های برنامه که از دو کلاس تشکیل شده :

CustomTextView     و     MainActivity

[align=left]

package com.sfh.dsfg;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView {
private static final String FONT = "font";
private static final String SIZE = "size";
private static final String COLOR = "color";
SharedPreferences sp;

private void inti(Context context) {
	sp=PreferenceManager.getDefaultSharedPreferences(context);
	Typeface face=Typeface.createFromAsset(context.getAssets(), sp.getString(FONT, "zar.ttf")); 
    	this.setTypeface(face); 
    	this.setTextSize(sp.getInt(SIZE, 15));
    	this.setTextColor(Color.parseColor(sp.getString(COLOR, "#000000")));
}
   public CustomTextView(Context context) {
     super(context);
     inti(context);
        }

   

public CustomTextView(Context context, AttributeSet attrs) {
       super(context, attrs);
       inti(context);
   }

   public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
       inti(context);
   }

   protected void onDraw (Canvas canvas) {
       super.onDraw(canvas);
       inti(getContext());

      
   }

}

[/align]

[align=right]اینم کد های اکتیویتی اصلی:[/align]

[align=left]

 private static final String SIZE = "size";
private static final String FONT = "font";
private static final String COLOR = "color";
private SeekBar                       seekbar;
private SharedPreferences sp;
private TextView txt_sample;
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.set);

	sp=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
	 seekbar = (SeekBar) findViewById(R.id.seekBar1);
        seekbar.setMax(50);
        seekbar.setProgress(sp.getInt(SIZE, 15));
        txt_sample = (TextView) findViewById(R.id.txt);
        txt_sample.setTextSize(sp.getInt(SIZE, 15));
        
        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //add here your implementation 
            }



            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //add here your implementation
            }



            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
              //  txt_sample.setTextSize(progress);
                sp.edit().putInt(SIZE, progress).commit();
            }
        });
}



public void onFontClick(View v) {

      RadioGroup rg = (RadioGroup) v.getParent();
      String selectItem ="BZar.ttf";

      switch (rg.getCheckedRadioButtonId()) {
          case R.id.radio0:
              selectItem = "BZar.ttf";
              break;
          case R.id.radio1:
              selectItem ="persian.ttf";
              break;
      }
      sp.edit().putString(FONT, selectItem).commit();
  }
public void onColorClick(View v) {

      RadioGroup rg = (RadioGroup) v.getParent();
      String selectItem ="#ff0000";

      switch (rg.getCheckedRadioButtonId()) {
          case R.id.rd_red:
              selectItem = "#ff0000";
              break;
          case R.id.rd_green:
              selectItem ="#00ff00";
              break;
      }
      sp.edit().putString(COLOR, selectItem).commit();
  }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.set, menu);
	return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case android.R.id.home:
		// This ID represents the Home or Up button. In the case of this
		// activity, the Up button is shown. Use NavUtils to allow users
		// to navigate up one level in the application structure. For
		// more details, see the Navigation pattern on Android Design:
		//
		// http://developer.android.com/design/patterns/navigation.html#up-vs-back
		//
		NavUtils.navigateUpFromSameTask(this);
		return true;
	}
	return super.onOptionsItemSelected(item);
}

}

[/align]

اینم xml اکتیویتی اصلی :

[align=left]

            android:id="@+id/txt"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="سلام ایرانی"
       android:textColor="#000000" />

           android:id="@+id/seekBar1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

           android:id="@+id/radioGroup1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >

                   android:id="@+id/radio0"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:checked="true"
           android:onClick="onFontClick"
           android:text="Bzar" />

                   android:id="@+id/radio1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:onClick="onFontClick"
           android:text="Persian" />
   

   android:id="@+id/radioGroup2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" >

           android:id="@+id/rd_red"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:checked="true"
       android:onClick="onColorClick"
       android:text="red" />

           android:id="@+id/rd_green"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:onClick="onColorClick"
       android:text="green" />


[/align]

لینک ارسال
به اشتراک گذاری در سایت های دیگر

فونت مورد نظر رو تو پوشه asset قرار دادی؟

آره

همه چیزش تکمیله...........

فقط نمیدونم چرا فورس کلوز میزنه!!!!!!!!!!!!!!!:-/:-/:-/

لینک ارسال
به اشتراک گذاری در سایت های دیگر

من کلاس CustomTextView رو تو تاپیک آموزشی مورد نظر ویرایش کردم، کدت رو آپدیت کن. شاید مشکل از همون باشه.

لینک ارسال
به اشتراک گذاری در سایت های دیگر

سلام

دوست عزیز توی پوشه assets هر دو فونت zar و BZar و دارین؟ توی کدت دوتا فونت زر نوشته شده

سلام

مشکل از اونجا نیست

چون توی پوشه assets هم دو تا زر دارم............

لینک ارسال
به اشتراک گذاری در سایت های دیگر

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...