在安卓上的微调器中设置古吉拉特文 [英] Set Gujarati text on Spinner in Android

查看:172
本文介绍了在安卓上的微调器中设置古吉拉特文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展的古吉拉特语的Andr​​oid应用程序,需要在微调控件设置古吉拉特语文字。于是,我学会了如何从的这里

但问题是我得到的,而不是实际的文本框。我的文字微调来自字符串资源。附加我的code。

 公共类SpinnerExActivity延伸活动{
微调mySpinner;
字样myFont;

的String []项目;
MyArrayAdapter马;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_spinner_ex);
    mySpinner =(微调)findViewById(R.id.spinner1);
     。项目= getResources()getStringArray(R.array.items);
     myFont = Typeface.createFromAsset(getAssets(),字体/ SHRUTIB.TTF);
     MA =新MyArrayAdapter(这一点,R.layout.my_spinner_style);
     ma.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


}
私有类MyArrayAdapter扩展ArrayAdapter {

    公共MyArrayAdapter(上下文的背景下,INT textViewResourceId){
    超(背景下,textViewResourceId);
    }

    公众的TextView getView(INT位置,查看convertView,ViewGroup中父){
    TextView的V =(TextView中)super.getView(位置,convertView,父母);
    v.setTypeface(myFont);
    v.setText(项目[位置]);
    返回伏;
    }

    公众的TextView getDropDownView(INT位置,查看convertView,ViewGroup中父){
    TextView的V =(TextView中)super.getView(位置,convertView,父母);
    v.setTypeface(myFont);
    返回伏;
    }

    }
 

当我尝试这个模拟器上,我得到框。但是,当我尝试在我的平板电脑我什么也没有。

任何帮助将是AP preciable。

修改 这部作品::感谢名单为<一个href="http://stackoverflow.com/questions/5483495/how-to-set-font-custom-font-to-spinner-text-programmatically">this.

 公共类SpinnerExActivity延伸活动{
微调mySpinner;
字样myFont;

的String []项目;


@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_spinner_ex);
    mySpinner =(微调)findViewById(R.id.spinner1);
     。项目= getResources()getStringArray(R.array.items);
     myFont = Typeface.createFromAsset(getAssets(),字体/ SHRUTIB.TTF);

     ArrayAdapter&LT;字符串&GT;适配器=新的ArrayAdapter&LT;字符串&GT;(这一点,
             R.layout.my_spinner_style,项目){

  公共查看getView(INT位置,查看convertView,ViewGroup中父){
          视图V = super.getView(位置,convertView,父母);

          字样externalFont = Typeface.createFromAsset(getAssets(),字体/ SHRUTIB.TTF);
          ((TextView中)V).setTypeface(externalFont);
          ((TextView中)V).setText(项目[位置]);
          ((TextView中)V).setTextColor(Color.BLACK);
          返回伏;
  }


  公共查看getDropDownView(INT位置,查看convertView,ViewGroup中父){
           视图V = super.getDropDownView(位置,convertView,父母);

          字样externalFont = Typeface.createFromAsset(getAssets(),字体/ SHRUTIB.TTF);
          ((TextView中)V).setTypeface(externalFont);
          ((TextView中)V).setText(项目[位置]);

          返回伏;
   }
   };


     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     mySpinner.setAdapter(适配器);

}
 

解决方案

您在看这个:

检查低于code:

 包com.example.main.util;

进口android.app.Activity;
进口android.graphics.Typeface;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.Spinner;
进口android.widget.TextView;

进口com.example.testapp.R;

公共类testActivity延伸活动{

    私有静态最后的String [] COUNTRIES =新的String [] {比利时,
            法国,意大利,德国,西班牙};
    私人微调mySpinner;
    私人字样myFont;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.newlay);

        mySpinner =(微调)findViewById(R.id.spinner1);
        myFont = Typeface.createFromAsset(getAssets(),gujarti.ttf);
        MyArrayAdapter MA =新MyArrayAdapter(本);
        mySpinner.setAdapter(MA);
    }

    私有类MyArrayAdapter扩展了BaseAdapter {

        私人LayoutInflater mInflater;

        公共MyArrayAdapter(testActivity CON){
            // TODO自动生成构造函数存根
            mInflater = LayoutInflater.from(CON);
        }

        @覆盖
        公众诠释getCount将(){
            // TODO自动生成方法存根
            返回COUNTRIES.length;
        }

        @覆盖
        公共对象的getItem(INT位置){
            // TODO自动生成方法存根
            返回的位置;
        }

        @覆盖
        众长getItemId(INT位置){
            // TODO自动生成方法存根
            返回的位置;
        }

        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            // TODO自动生成方法存根
            最后的listContent持有人;
            视图V = convertView;
            如果(V == NULL){
                V = mInflater.inflate(R.layout.my_spinner_style,NULL);
                持有人=新的listContent();

                holder.name =(TextView中)v.findViewById(R.id.textView1);

                v.setTag(保持器);
            } 其他 {

                支架=(的listContent)v.getTag();
            }

            holder.name.setTypeface(myFont);
            holder.name.setText(+国[位置]);

            返回伏;
        }

    }

    静态类的listContent {

        TextView的名称;

    }
}
 

祝您好运!

I am developing an Android app in Gujarati language, need to set Gujarati text on a Spinner widget. So, I learned how to set custom fonts on Spinner from HERE.

But the problem is I am getting boxes instead of actual text. My text for spinner comes from string resources. Attaching my code.

public class SpinnerExActivity extends Activity {
Spinner mySpinner; 
Typeface myFont;

String[] items; 
MyArrayAdapter ma;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_ex);
    mySpinner = (Spinner) findViewById(R.id.spinner1);
     items=getResources().getStringArray(R.array.items);
     myFont = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
     ma=new MyArrayAdapter(this, R.layout.my_spinner_style); 
     ma.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


}
private class MyArrayAdapter extends ArrayAdapter {

    public MyArrayAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    }

    public TextView getView(int position, View convertView, ViewGroup parent) {
    TextView v = (TextView) super.getView(position, convertView, parent);
    v.setTypeface(myFont);
    v.setText(items[position]);
    return v;
    }

    public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
    TextView v = (TextView) super.getView(position, convertView, parent);
    v.setTypeface(myFont);
    return v;
    }

    }

I get boxes when I try this on emulator. But when I try it on my tablet I get nothing.

Any help would be appreciable.

EDIT This Works:: Thanx to this.

public class SpinnerExActivity extends Activity {
Spinner mySpinner; 
Typeface myFont;

String[] items; 


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_ex);
    mySpinner = (Spinner) findViewById(R.id.spinner1);
     items=getResources().getStringArray(R.array.items);
     myFont = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             R.layout.my_spinner_style, items) {

  public View getView(int position, View convertView, ViewGroup parent) {
          View v = super.getView(position, convertView, parent);

          Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
          ((TextView) v).setTypeface(externalFont);
          ((TextView) v).setText(items[position]);
          ((TextView) v).setTextColor(Color.BLACK);
          return v;
  }


  public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
           View v =super.getDropDownView(position, convertView, parent);

          Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
          ((TextView) v).setTypeface(externalFont);
          ((TextView) v).setText(items[position]);

          return v;
   }
   };


     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                                 
     mySpinner.setAdapter(adapter);

}

解决方案

You have to look in to this:

check below code:

package com.example.main.util;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.testapp.R;

public class testActivity extends Activity {

    private static final String[] COUNTRIES = new String[] { "Belgium",
            "France", "Italy", "Germany", "Spain" };
    private Spinner mySpinner;
    private Typeface myFont;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newlay);

        mySpinner = (Spinner) findViewById(R.id.spinner1);
        myFont = Typeface.createFromAsset(getAssets(), "gujarti.ttf");
        MyArrayAdapter ma = new MyArrayAdapter(this);
        mySpinner.setAdapter(ma);
    }

    private class MyArrayAdapter extends BaseAdapter {

        private LayoutInflater mInflater;

        public MyArrayAdapter(testActivity con) {
            // TODO Auto-generated constructor stub
            mInflater = LayoutInflater.from(con);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return COUNTRIES.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final ListContent holder;
            View v = convertView;
            if (v == null) {
                v = mInflater.inflate(R.layout.my_spinner_style, null);
                holder = new ListContent();

                holder.name = (TextView) v.findViewById(R.id.textView1);

                v.setTag(holder);
            } else {

                holder = (ListContent) v.getTag();
            }

            holder.name.setTypeface(myFont);
            holder.name.setText("" + COUNTRIES[position]);

            return v;
        }

    }

    static class ListContent {

        TextView name;

    }
}

Best Luck!

这篇关于在安卓上的微调器中设置古吉拉特文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆