অ্যাকশনবার শিরোনামে কীভাবে কাস্টম ফন্ট সেট করবেন?


257

কীভাবে (সম্ভব হলে) আমি আমার সম্পদ ফোল্ডারে একটি ফন্ট সহ অ্যাকশনবার শিরোনাম পাঠ্যে (কেবল - ট্যাব পাঠ্য নয়) একটি কাস্টম ফন্ট সেট করতে পারি? আমি অ্যান্ড্রয়েড: লোগো বিকল্পটি ব্যবহার করতে চাই না।

উত্তর:


211

আমি সম্মত হই যে এটি সম্পূর্ণরূপে সমর্থিত নয়, তবে আমি যা করেছি তা এখানে। আপনি আপনার অ্যাকশন বারের জন্য একটি কাস্টম ভিউ ব্যবহার করতে পারেন (এটি আপনার আইকন এবং আপনার ক্রিয়া আইটেমগুলির মধ্যে প্রদর্শিত হবে) display আমি একটি কাস্টম ভিউ ব্যবহার করছি এবং আমার নেটিভ শিরোনাম অক্ষম আছে। আমার সমস্ত ক্রিয়াকলাপ একক ক্রিয়াকলাপ থেকে উত্তরাধিকার সূত্রে আসে, এতে অনক্রিটটিতে এই কোড রয়েছে:

this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);

LayoutInflater inflator = LayoutInflater.from(this);
View v = inflator.inflate(R.layout.titleview, null);

//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());

//assign the view to the actionbar
this.getActionBar().setCustomView(v);

এবং আমার লেআউট এক্সএমএল (উপরের কোডে R.layout.titleview) এর মত দেখাচ্ছে:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" >

<com.your.package.CustomTextView
        android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:textSize="20dp"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="" />
</RelativeLayout>

1
এটি শিরোনামটির জন্য দুর্দান্ত কাজ করে তবে আপনি যদি কোনও শিরোনাম চান এবং ট্যাবগুলি এটি কাস্টম ভিউটিকে শিরোনামের মতো বামে না থাকা ট্যাবগুলির ডানদিকে রাখে। আমি প্রকৃত শিরোনাম পরিবর্তন করতে সক্ষম হতে চাই।
ড্রাক্সিয়া

2
দুর্দান্ত সমাধান। আপনার যদি কোনও কাস্টম টেক্সট ভিউ ক্লাস প্রয়োজন হয় যা এক্সএমএলে ফন্টের স্পেসিফিকেশনকে অনুমতি দেয় তবে দয়া করে আমার চেষ্টা করুন! github.com/tom-dignan/nifty - এটি খুব সহজ।
থমাস ডিগানান

এই কোডটি কি অনক্রিট () এ থাকতে হবে? আমাকে আমার ক্রিয়াকলাপের বাইরে এটি গতিশীলভাবে সেট করা দরকার ...
ইগোরগানাপলস্কি

আপনার গতিশীল ফন্ট পরিবর্তন করতে হবে? অথবা আপনি একবারে ফন্টটি ইতিমধ্যে কাস্টমাইজ হয়ে গেলে শিরোনাম পরিবর্তন করতে চাইছেন?
স্যাম ডোজর

2
এটি কাজ করে, তবে এটি অনেক কাজ করার উপায়। এছাড়াও: আপনি স্ট্যান্ডার্ড শিরোনামের কিছু বৈশিষ্ট্য হারাবেন, যেমন আইকনটি ক্লিক করার সময় এটি হাইলাইট করা হয়েছে ... কাস্টম শিরোনামগুলি কেবল হরফগুলি পরিবর্তন করার জন্য মানক শিরোনাম বিন্যাসটি পুনরায় তৈরি করতে ব্যবহার করা হয় না ...
জর্ডিড

422

আপনি এটি একটি কাস্টম TypefaceSpanক্লাস ব্যবহার করে করতে পারেন । এটি customViewউপরে উল্লিখিত পদ্ধতির চেয়ে উচ্চতর কারণ ক্রিয়াকলাপের দৃশ্যগুলি প্রসারিত করার মতো অন্যান্য অ্যাকশন বার উপাদান ব্যবহার করার সময় এটি ভেঙে যায় না।

এই জাতীয় শ্রেণীর ব্যবহার দেখতে এরকম কিছু দেখায়:

SpannableString s = new SpannableString("My Title");
s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(),
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getActionBar();
actionBar.setTitle(s);

কাস্টম TypefaceSpanক্লাসটি আপনার ক্রিয়াকলাপের প্রসঙ্গ এবং আপনার assets/fontsডিরেক্টরিতে একটি টাইপফেসের নামটি পাস করেছে । এটি ফাইলটি লোড করে এবং Typefaceমেমোরিতে একটি নতুন উদাহরণকে ক্যাশে করে । সম্পূর্ণ বাস্তবায়ন TypefaceSpanআশ্চর্যজনকভাবে সহজ:

/**
 * Style a {@link Spannable} with a custom {@link Typeface}.
 * 
 * @author Tristan Waddington
 */
public class TypefaceSpan extends MetricAffectingSpan {
      /** An <code>LruCache</code> for previously loaded typefaces. */
    private static LruCache<String, Typeface> sTypefaceCache =
            new LruCache<String, Typeface>(12);

    private Typeface mTypeface;

    /**
     * Load the {@link Typeface} and apply to a {@link Spannable}.
     */
    public TypefaceSpan(Context context, String typefaceName) {
        mTypeface = sTypefaceCache.get(typefaceName);

        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getApplicationContext()
                    .getAssets(), String.format("fonts/%s", typefaceName));

            // Cache the loaded Typeface
            sTypefaceCache.put(typefaceName, mTypeface);
        }
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}

উপরের শ্রেণিটি কেবল আপনার প্রকল্পে অনুলিপি করুন এবং এটি আপনার ক্রিয়াকলাপের onCreateপদ্ধতিতে উপরে প্রদর্শিত হিসাবে প্রয়োগ করুন ।


20
চমৎকার উত্তর. দেখতে ভাল কি তা আপনি টাইপফেস উপাদানটি ক্যাশে করার একটি উপায়ও দেখিয়েছেন।
আনন্দ সাইনাথ

6
এটি দুর্দান্ত। একটি গোছা - যদি textAllCapsবৈশিষ্ট্যটি অন্তর্ভুক্ত টেক্সটভিউতে সত্য হিসাবে সেট করা হয় (যেমন কোনও থিমের মাধ্যমে), তবে কাস্টম ফন্টটি উপস্থিত হবে না। আমি যখন এই কৌশলটি অ্যাকশন বার ট্যাব আইটেমগুলিতে প্রয়োগ করি তখন এটি আমার জন্য একটি সমস্যা ছিল।
জেমস

4
নোট করুন যে শ্রেণীর এই বাস্তবায়ন অনুমান করে যে আপনি নিজের ফন্ট ফাইলগুলি এতে রেখেছেন assets/fonts/। আপনি শুধুমাত্র একটি subfolder সম্পদ অধীনে এবং .ttf / .otf ফাইল নিক্ষেপ করেন, তাহলে আপনি কোডের নিম্নলিখিত লাইন অনুসারে সংশোধন করা উচিত: String.format("fonts/%s", typefaceName)। আমি এটি বের করার চেষ্টা করে 10 মিনিট ভাল করে ফেলেছি। আপনি যদি তা না করেন তবে আপনি পাবেনjava.lang.RuntimeException: Unable to start activity ComponentInfo{com.your.pckage}: java.lang.RuntimeException: native typeface cannot be made
Dzhuneyt

1
অ্যাপ্লিকেশনটি শুরু করার মুহুর্তে, ডিফল্ট শিরোনাম শৈলীর দৃশ্যমান এবং প্রায় 1 সেকেন্ড পরে কাস্টম শৈলী উপস্থিত হয়। খারাপ ইউআই ...
উপবিষ্ট করুন

2
এটি একটি দুর্দান্ত উত্তর এবং আমাকে একটি টন সাহায্য করেছিল। আমি যে উন্নতি যুক্ত করব তা হ'ল টাইপফেসস্প্যানের বাইরে ক্যাচিং ব্যবস্থাটি নিজস্ব শ্রেণিতে স্থানান্তর করা। আমি অন্যান্য পরিস্থিতিতে ছুঁড়েছি যেখানে আমি স্প্যান ছাড়াই টাইপফেস ব্যবহার করছি এবং এটি আমাকে সেই পরিস্থিতিতেও ক্যাশের সুবিধা নিতে পেরেছিল।
জাস্টিন

150
int titleId = getResources().getIdentifier("action_bar_title", "id",
            "android");
    TextView yourTextView = (TextView) findViewById(titleId);
    yourTextView.setTextColor(getResources().getColor(R.color.black));
    yourTextView.setTypeface(face);

2
এটি প্রশ্নের উত্তর হওয়া উচিত be "অ্যাকশন_বার_সুবিটাইটেল" দিয়েও দুর্দান্ত কাজ করে! ধন্যবাদ!
জোর্ডিড

20
যদি নতুন সংস্করণে অ্যান্ড্রয়েড বিকাশকারীরা রিসোর্স আইডিটিকে "অ্যাকশন_বার_টাইটেল" থেকে অন্য নামে পরিবর্তন করে, তবে এর কিছুই কার্যকর হবে না। এ কারণেই এতটা ভোট হয়নি।
ডায়োগো বেন্টো

6
এপিআই> 3.0 এ কাজ করে তবে অ্যাপক্যাম্পেটের জন্য 2.x এ নয়
আমান

1
এটি ফন্ট এবং সব কিছু পরিবর্তন করে। তবে যখন আমি পরবর্তী ক্রিয়াকলাপটি ফিরে যাব এবং ফিরে চাপুন তখন ফন্টটি উল্টে যায়। আমার ধারণা এটি অ্যাকশনবারের বৈশিষ্ট্যগুলির সাথে কিছু করার আছে।
প্রণব মহাজন

11
@ ডিজিট: এটি "হলো থিম" এর জন্য দুর্দান্ত কাজ করেছে তবে "ম্যাটেরিয়াল থিম" (অ্যান্ড্রয়েড এল) এর পক্ষে নয় doesn't শিরোনামআইড পাওয়া গেছে, তবে পাঠ্যদর্শনটি নাল .. কোনও ধারণা কীভাবে এটি ঠিক করবেন? ধন্যবাদ!
মাইকেল ডি

34

থেকে অ্যান্ড্রয়েড সাপোর্ট লাইব্রেরী v26 + + অ্যান্ড্রয়েড স্টুডিও 3.0 অগ্রে, এই প্রক্রিয়া সহজ একটি ঝাঁকুনি যেমন পরিণত হয়েছে !!

টুলবার শিরোনামের ফন্টটি পরিবর্তন করতে এই পদক্ষেপগুলি অনুসরণ করুন:

  1. পড়ুন ডাউনলোডযোগ্য ফন্ট তালিকা (থেকে কোন ফন্ট নির্বাচন আমার সুপারিশ ) অথবা একটি কাস্টম ফন্ট লোড res > fontঅনুযায়ী XML- এর ফন্ট
  2. ইন res > values > styles, নিম্নলিখিত পেস্ট করুন ( আপনার কল্পনা এখানে ব্যবহার করুন! )

    <style name="TitleBarTextAppearance" parent="android:TextAppearance">
        <item name="android:fontFamily">@font/your_desired_font</item>
        <item name="android:textSize">23sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@android:color/white</item>
    </style>
  3. app:titleTextAppearance="@style/TextAppearance.TabsFont"নীচে দেখানো অনুসারে আপনার সরঞ্জামদণ্ডে বৈশিষ্ট্যগুলিতে একটি নতুন লাইন প্রবেশ করান

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextAppearance="@style/TitleBarTextAppearance"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>
  4. কাস্টম অ্যাকশনবার শিরোনাম ফন্ট স্টাইল উপভোগ করুন !!


2
এটি সরঞ্জামদণ্ডের জন্য দুর্দান্ত। এই অ্যাপ্লিকেশনটি করার কোনও উপায় যেমন আপনার নতুন ক্রিয়াকলাপে যখন ডিফল্ট অ্যাপ বার থাকে তখন?
জর্দান এইচ

14

হাতের লেখা গ্রন্থাগার আপনাকে অ্যাপ্লিকেশন থিম, যা কর্ম বার প্রযোজ্য হবে মাধ্যমে একটি কাস্টম ফন্ট নির্ধারণ করে না।

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>
</style>

<style name="AppTheme.Widget"/>

<style name="AppTheme.Widget.TextView" parent="android:Widget.Holo.Light.TextView">
   <item name="fontPath">fonts/Roboto-ThinItalic.ttf</item>
</style>

ক্যালিগ্রাফি সক্রিয় করতে যা লাগে তা এটিকে আপনার কার্যকলাপের প্রসঙ্গে যুক্ত করে:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(new CalligraphyContextWrapper(newBase));
}

ডিফল্ট কাস্টম বৈশিষ্ট্যটি হ'ল fontPathতবে আপনি অ্যাপ্লিকেশন শ্রেণিতে এটি দিয়ে আরম্ভ করে পথের জন্য আপনার নিজস্ব কাস্টম বৈশিষ্ট্যটি সরবরাহ করতে পারেন CalligraphyConfig.Builder। ব্যবহার android:fontFamilyনিরুৎসাহিত করা হয়েছে।


এই সমাধানের জন্য ন্যূনতম এপিআই 16
সামি এলটামাভি

প্রকল্পের বিল্ড ফাইল অনুযায়ী minSdk 7, তবে আমি এটি একটি minSdk 18 প্রকল্পে ব্যবহার করছি এবং এর উপর আর কোনও চেকিং করিনি। আপত্তিজনক পদ্ধতিটি কী ব্যবহৃত হয়?
থাউটব্যাকার্স

এর সর্বনিম্ন এপিআই 7, কেবল উদাহরণটি API16 API এটি অ্যাপকম্প্যাট-ভি 7 + সমর্থন করে
ক্রিস.জেনকিনস

11

এটি একটি কুরুচিপূর্ণ হ্যাক তবে আপনি এটি এটি করতে পারেন (যেহেতু অ্যাকশন_বার_টাইটেলটি লুকানো রয়েছে):

    try {
        Integer titleId = (Integer) Class.forName("com.android.internal.R$id")
                .getField("action_bar_title").get(null);
        TextView title = (TextView) getWindow().findViewById(titleId);
        // check for null and manipulate the title as see fit
    } catch (Exception e) {
        Log.e(TAG, "Failed to obtain action bar title reference");
    }

এই কোডটি জিনবারব্রেড পরবর্তী ডিভাইসের জন্য তবে এটি অ্যাকশনবারার শেরলকের সাথে কাজ করার জন্য সহজেই প্রসারিত করা যেতে পারে

পিএস @ পিজেভি মন্তব্যের ভিত্তিতে অ্যাকশন বারের শিরোনাম আইডি খুঁজে পাওয়ার আরও ভাল উপায় আছে

final int titleId = 
    Resources.getSystem().getIdentifier("action_bar_title", "id", "android");

4
আমি dtmilano এর উত্তর পছন্দ stackoverflow.com/questions/10779037/... । এটি অনুরূপ তবে সামান্য আরও ভবিষ্যতের প্রমাণ।
pjv

1
@pjv - সম্মত "হ্যাকি" কম মনে হচ্ছে। আমি আমার উত্তরটি পরিবর্তন করেছি
বোস্টন

1
সুতরাং প্রশ্নটি কাস্টম ফন্ট সম্পর্কে। এটি ডিফল্ট অ্যাকশনবারের পাঠ্য ভিউটি কীভাবে পাবেন তা উত্তর দেয় ।
অ্যালিকেলিন-কিলাকা

@ কিলাকা - ধারণাটি ছিল যে আপনি যদি পাঠ্য দর্শনের সেটিংসটি পাবেন তবে কাস্টম ফন্টটি তুচ্ছ হবে। যদিও এটি একটি পুরাতন পোস্ট, আমার কাছে মনে হয় টুইডডিংটন উত্তরটি অনেক বেশি পছন্দ
বোস্টোন

8

নিম্নলিখিত কোডটি সমস্ত সংস্করণের জন্য কাজ করবে। আমি এটিকে জিঞ্জারব্রেডযুক্ত ডিভাইসে পাশাপাশি জেলিবিয়ান ডিভাইসে চেক করেছিলাম

 private void actionBarIdForAll()
    {
        int titleId = 0;

        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
        {
            titleId = getResources().getIdentifier("action_bar_title", "id", "android");
        }
        else
        {
          // This is the id is from your app's generated R class when ActionBarActivity is used for SupportActionBar

            titleId = R.id.action_bar_title;
        }

        if(titleId>0)
        {
            // Do whatever you want ? It will work for all the versions.

            // 1. Customize your fonts
            // 2. Infact, customize your whole title TextView

            TextView titleView = (TextView)findViewById(titleId);
            titleView.setText("RedoApp");
            titleView.setTextColor(Color.CYAN);
        }
    }

এটি আমার জন্য অ্যাকশনবার এবং অ্যাপকম্প্যাট অ্যাকশনবার উভয়ের জন্যই কাজ করে। তবে পরেরগুলি কেবল তখনই কাজ করে যদি আমি অনক্রিট () এর পরে শিরোনাম দৃশ্যটি সন্ধান করার চেষ্টা করি, সুতরাং উদাহরণস্বরূপ এটি পোস্টপ্রেট () তে রেখে দেওয়া কৌশলটি করে।
হারি

8

সমর্থন লাইব্রেরিতে নতুন টুলবারটি আপনার অ্যাকশনবারটিকে আপনার নিজের হিসাবে ডিজাইন করুন বা কোডের নীচে ব্যবহার করুন

স্প্যানিয়েবল স্ট্রিং বিল্ডার ব্যবহার করে টেক্সটভিউ স্ফীত করা ভাল বিকল্প নয়

Typeface font2 = Typeface.createFromAsset(getAssets(), "fonts/<your font in assets folder>");   
SpannableStringBuilder SS = new SpannableStringBuilder("MY Actionbar Tittle");
SS.setSpan (new CustomTypefaceSpan("", font2), 0, SS.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
actionBar.setTitle(ss);

শ্রেণীর নীচে অনুলিপি

public class CustomTypefaceSpan extends TypefaceSpan{

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }

}

7
    ActionBar actionBar = getSupportActionBar();
    TextView tv = new TextView(getApplicationContext());
    Typeface typeface = ResourcesCompat.getFont(this, R.font.monotype_corsiva);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, // Width of TextView
            RelativeLayout.LayoutParams.WRAP_CONTENT); // Height of TextView
    tv.setLayoutParams(lp);
    tv.setText("Your Text"); // ActionBar title text
    tv.setTextSize(25);
    tv.setTextColor(Color.WHITE);
    tv.setTypeface(typeface, typeface.ITALIC);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setCustomView(tv);

দুর্দান্ত এটি পুরোপুরি কাজ করছে আমি কীভাবে এই অ্যাপ বারটিকে কেন্দ্রে প্রবেশ করতে পারি?
প্রসথ

একটি যাদুমন্ত্র মত কাজ .. শুধু প্রতিস্থাপন typeface.ITALICসঙ্গে Typeface.ITALICকোন স্ট্যাটিক সদস্য সতর্কবার্তা আছে
জৈন

3

আপনি যদি পুরো ক্রিয়াকলাপের সমস্ত পাঠ্য ভিউতে টাইপফেস সেট করতে চান তবে আপনি এই জাতীয় কিছু ব্যবহার করতে পারেন:

public static void setTypefaceToAll(Activity activity)
{
    View view = activity.findViewById(android.R.id.content).getRootView();
    setTypefaceToAll(view);
}

public static void setTypefaceToAll(View view)
{
    if (view instanceof ViewGroup)
    {
        ViewGroup g = (ViewGroup) view;
        int count = g.getChildCount();
        for (int i = 0; i < count; i++)
            setTypefaceToAll(g.getChildAt(i));
    }
    else if (view instanceof TextView)
    {
        TextView tv = (TextView) view;
        setTypeface(tv);
    }
}

public static void setTypeface(TextView tv)
{
    TypefaceCache.setFont(tv, TypefaceCache.FONT_KOODAK);
}

এবং টাইপফেসক্যাচ:

import java.util.TreeMap;

import android.graphics.Typeface;
import android.widget.TextView;

public class TypefaceCache {

    //Font names from asset:
    public static final String FONT_ROBOTO_REGULAR = "fonts/Roboto-Regular.ttf";
    public static final String FONT_KOODAK = "fonts/Koodak.ttf";

    private static TreeMap<String, Typeface> fontCache = new TreeMap<String, Typeface>();

    public static Typeface getFont(String fontName) {
        Typeface tf = fontCache.get(fontName);
        if(tf == null) {
            try {
                tf = Typeface.createFromAsset(MyApplication.getAppContext().getAssets(), fontName);
            }
            catch (Exception e) {
                return null;
            }
            fontCache.put(fontName, tf);
        }
        return tf;
    }

    public static void setFont(TextView tv, String fontName)
    {
        tv.setTypeface(getFont(fontName));
    }
}

3

আমি স্রেফ অনক্রিট () ফাংশনের ভিতরে নিম্নলিখিতটি করেছি:

TypefaceSpan typefaceSpan = new TypefaceSpan("font_to_be_used");
SpannableString str = new SpannableString("toolbar_text");
str.setSpan(typefaceSpan,0, str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(str);

আমি সাপোর্ট লাইব্রেরিগুলি ব্যবহার করছি, আপনি যদি সেগুলি ব্যবহার না করেন তবে আমার ধারণা, আপনার getSupportActionBar () এর পরিবর্তে getActionBar () এ স্যুইচ করা উচিত।

অ্যান্ড্রয়েড স্টুডিও 3 এ আপনি এই নির্দেশাবলী অনুসরণ করে কাস্টম ফন্টগুলি যুক্ত করতে পারেন https://developer.android.com/guide/topics/ui/look-and-feel/fouts-in-xml.html এবং তারপরে আপনার নতুন যুক্ত করা ফন্টটি " font_to_be_used "


1

@ স্যাম_ডির উত্তরে যুক্ত করতে, এটির কাজটি করার জন্য আমাকে এটি করতে হয়েছিল:

this.setTitle("my title!");
((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
TextView title = ((TextView)v.findViewById(R.id.title));
title.setEllipsize(TextUtils.TruncateAt.MARQUEE);
title.setMarqueeRepeatLimit(1);
// in order to start strolling, it has to be focusable and focused
title.setFocusable(true);
title.setSingleLine(true);
title.setFocusableInTouchMode(true);
title.requestFocus();

এটি ওভারকিলের মতো বলে মনে হচ্ছে - v.findViewById (R.id.title) দু'বার রেফারেন্সিং - তবে এটিই আমাকে এটি করতে দেয় that's


1

সঠিক উত্তর আপডেট করতে।

প্রথমত: শিরোনামটিকে মিথ্যাতে সেট করুন, কারণ আমরা কাস্টম ভিউটি ব্যবহার করছি

    actionBar.setDisplayShowTitleEnabled(false);

দ্বিতীয়ত: টাইটেলভিউ.এক্সএমএল তৈরি করুন

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/transparent" >

    <TextView
       android:id="@+id/title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true"
       android:layout_marginLeft="10dp"
       android:textSize="20dp"
       android:maxLines="1"
       android:ellipsize="end"
       android:text="" />

</RelativeLayout>

শেষ অবধি:

//font file must be in the phone db so you have to create download file code
//check the code on the bottom part of the download file code.

   TypeFace font = Typeface.createFromFile("/storage/emulated/0/Android/data/"   
    + BuildConfig.APPLICATION_ID + "/files/" + "font name" + ".ttf");

    if(font != null) {
        LayoutInflater inflator = LayoutInflater.from(this);
        View v = inflator.inflate(R.layout.titleview, null);
        TextView titleTv = ((TextView) v.findViewById(R.id.title));
        titleTv.setText(title);
        titleTv.setTypeface(font);
        actionBar.setCustomView(v);
    } else {
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("  " + title); // Need to add a title
    }

ফন্ট ফাইল ডাউনলোড করুন: কারণ আমি ফাইলটি ক্লাউডোনারিতে সংরক্ষণ করছি তাই এটি ডাউনলোড করার জন্য আমার এতে লিঙ্ক রয়েছে।

/**downloadFile*/
public void downloadFile(){
    String DownloadUrl = //url here
    File file = new File("/storage/emulated/0/Android/data/" + BuildConfig.APPLICATION_ID + "/files/");
    File[] list = file.listFiles();
    if(list == null || list.length <= 0) {
        BroadcastReceiver onComplete = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                try{
                    showContentFragment(false);
                } catch (Exception e){
                }
            }
        };

        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadUrl));
        request.setVisibleInDownloadsUi(false);
        request.setDestinationInExternalFilesDir(this, null, ModelManager.getInstance().getCurrentApp().getRegular_font_name() + ".ttf");
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);
    } else {
        for (File files : list) {
            if (!files.getName().equals("font_name" + ".ttf")) {
                BroadcastReceiver onComplete = new BroadcastReceiver() {
                    @Override
                    public void onReceive(Context context, Intent intent) {
                        try{
                            showContentFragment(false);
                        } catch (Exception e){
                        }
                    }
                };

                registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DownloadUrl));
                request.setVisibleInDownloadsUi(false);
                request.setDestinationInExternalFilesDir(this, null, "font_name" + ".ttf");
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            } else {
                showContentFragment(false);
                break;
            }
        }
    }
}

1

কোন কাস্টম পাঠ্যদর্শন প্রয়োজন!

প্রথমে আপনার জাভা কোডটিতে টোবারে শিরোনামটি অক্ষম করুন: getSupportActionBar () সেটডিসপ্লেশোটিটেলইনেবল (মিথ্যা);

তারপরে, সরঞ্জামদণ্ডের ভিতরে কেবল একটি টেক্সটভিউ যুক্ত করুন:

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="18sp"
        android:fontFamily="@font/roboto" />

    </android.support.v7.widget.Toolbar>

এটি সর্বশেষ নেভিগেশন ইউআই জেটপ্যাক লাইব্রেরিগুলির সাথে কাজ করবে না
আলী আশির

1

এটি ব্যবহার করার চেষ্টা করুন

TextView headerText= new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
headerText.setLayoutParams(lp);
headerText.setText("Welcome!");
headerText.setTextSize(20);
headerText.setTextColor(Color.parseColor("#FFFFFF"));
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/wesfy_regular.ttf");
headerText.setTypeface(tf);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(headerText);

0

এটি অর্জনের জন্য আমাদের প্রতিচ্ছবি ব্যবহার করতে হবে

final int titleId = activity.getResources().getIdentifier("action_bar_title", "id", "android");

    final TextView title;
    if (activity.findViewById(titleId) != null) {
        title = (TextView) activity.findViewById(titleId);
        title.setTextColor(Color.BLACK);
        title.setTextColor(configs().getColor(ColorKey.GENERAL_TEXT));
        title.setTypeface(configs().getTypeface());
    } else {
        try {
            Field f = bar.getClass().getDeclaredField("mTitleTextView");
            f.setAccessible(true);
            title = (TextView) f.get(bar);
            title.setTextColor(Color.BLACK);
            title.setTypeface(configs().getTypeface());
        } catch (NoSuchFieldException e) {
        } catch (IllegalAccessException e) {
        }
    }

-1

এটা চেষ্টা কর

public void findAndSetFont(){
        getActionBar().setTitle("SOME TEST TEXT");
        scanForTextViewWithText(this,"SOME TEST TEXT",new SearchTextViewInterface(){

            @Override
            public void found(TextView title) {

            } 
        });
    }

public static void scanForTextViewWithText(Activity activity,String searchText, SearchTextViewInterface searchTextViewInterface){
    if(activity == null|| searchText == null || searchTextViewInterface == null)
        return;
    View view = activity.findViewById(android.R.id.content).getRootView();
    searchForTextViewWithTitle(view, searchText, searchTextViewInterface);
}

private static void searchForTextViewWithTitle(View view, String searchText, SearchTextViewInterface searchTextViewInterface)
{
    if (view instanceof ViewGroup)
    {
        ViewGroup g = (ViewGroup) view;
        int count = g.getChildCount();
        for (int i = 0; i < count; i++)
            searchForTextViewWithTitle(g.getChildAt(i), searchText, searchTextViewInterface);
    }
    else if (view instanceof TextView)
    {
        TextView textView = (TextView) view;
        if(textView.getText().toString().equals(searchText))
            if(searchTextViewInterface!=null)
                searchTextViewInterface.found(textView);
    }
}
public interface SearchTextViewInterface {
    void found(TextView title);
}
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.