如何为自定义baseadapter类中的项目设置自定义listview字体

时间:2021-03-27 00:24:51

I have a custom listview baseadapter class that has a listview of installed apps on the phone.

我有一个定制的listview baseadapter类,它有一个安装在手机上的应用程序的listview。

如何为自定义baseadapter类中的项目设置自定义listview字体

Essentially I want to set a custom font for the appname in the listview. I tried to set like this with the id and setting the typeface; however, it was unable to be resolved/done in everywhere in my baseadapter class.

本质上,我想为listview中的appname设置一个自定义字体。我试着像这样设置id和设置字体;但是,在我的baseadapter类中,它无法在任何地方得到解决。

appname = (TextView)findViewById(R.id.appname);
appname.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/raleway-regular.otf"));

I have done a lot of research

我做了很多研究

How to set custom font for android listview?

如何为android listview设置自定义字体?

But this is a really weird situation with my baseadapter and needs, so I have been unable to produce a solution so far.

但这是一个非常奇怪的情况,我的基适配器和需要,所以我至今无法产生一个解决方案。

BaseAdapter class:

BaseAdapter类:

public class ApkAdapter extends BaseAdapter {

    //Pastebin link:  http://pastebin.com/LGRicg4U , http://pastebin.com/c4WfmhMK , http://pastebin.com/gFuuM4dY, http://pastebin.com/4Q7EP9G4
    // http://pastebin.com/Te2g072w,  http://pastebin.com/NLT5iUiA ,

    SharedPreferences sharedPrefs;
    SharedPreferences sharedPrefsapp;

    List<PackageInfo> packageList;
    TextView appnamestyle;

    Activity context;
    PackageManager packageManager;
    boolean[] itemChecked;
    HashSet checked;

    String PACKAGE_NAME;

    TextView appname;

    public ApkAdapter(Activity context, List<PackageInfo> packageList,
                      PackageManager packageManager) {
        super();
        this.context = context;

        this.packageList = packageList;
        this.packageManager = packageManager;
        itemChecked = new boolean[packageList.size()];
    }
    private class ViewHolder {
        TextView apkName;
        CheckBox ck1;
        TextView packageName;
    }

    public int getCount() {
        return packageList.size();
    }

    public Object getItem(int position) {
        return packageList.get(position);
    }

    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        LayoutInflater inflater = context.getLayoutInflater();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.installed_apps, null);
            holder = new ViewHolder();

            holder.apkName = (TextView) convertView.findViewById(R.id.appname);
            holder.ck1= (CheckBox)convertView.findViewById(R.id.checkBox1);
            holder.packageName = (TextView) convertView.findViewById(R.id.app_package);
            convertView.setTag(holder);
            //holder.ck1.setTag(packageList.get(position));
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        // ViewHolder holder = (ViewHolder) convertView.getTag();
        final PackageInfo packageInfo = (PackageInfo) getItem(position);

        Drawable appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo);

        // Make sure to define it again!
        PACKAGE_NAME = packageInfo.packageName;

        final String appName = packageManager.getApplicationLabel(packageInfo.applicationInfo).toString();
        appIcon.setBounds(0, 0, 80, 80);
        holder.apkName.setCompoundDrawables(appIcon, null, null, null);
        holder.apkName.setCompoundDrawablePadding(15);
        holder.apkName.setText(appName);
        //holder.packageName.setText(PACKAGE_NAME);

        holder.ck1.setChecked(false);

        if (itemChecked[position])
            holder.ck1.setChecked(true);
        else
            holder.ck1.setChecked(false);

        // CHANGE UP EVERYTHING! MAKE THIS SHIT WORK, TIGGA!
        checked = new HashSet();

        PACKAGE_NAME = packageInfo.packageName;
        // Log.d("just here: ", PACKAGE_NAME);

        sharedPrefs = context.getSharedPreferences(context.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
        sharedPrefsapp = context.getSharedPreferences("appdb", Context.MODE_PRIVATE);

        holder.ck1.setChecked(sharedPrefs.getBoolean(PACKAGE_NAME,false));
        holder.ck1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = context.getSharedPreferences(context.getApplicationContext().getPackageName(), Context.MODE_PRIVATE).edit();
                SharedPreferences.Editor editorapp = context.getSharedPreferences("appdb", Context.MODE_PRIVATE).edit();

                if (holder.ck1.isChecked()) {
                    itemChecked[position] = true;
                    holder.ck1.setChecked(true);
                    editor.putBoolean(packageInfo.packageName, true);
                    editorapp.putString(packageInfo.packageName, packageInfo.packageName);
                    editor.apply();
                    editorapp.apply();

                   // sharedPrefs = context.getSharedPreferences(context.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
                } else {
                    itemChecked[position] = false;
                    holder.ck1.setChecked(false);
                    editor.putBoolean(packageInfo.packageName, false);
                    editorapp.remove(packageInfo.packageName);
                    editor.apply();
                    editorapp.apply();
                    //sharedPrefs = context.getSharedPreferences(context.getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
                }

            }
        });
        return convertView;
    }
}

installedapps.xml :

installedapps。xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

    <ListView
        android:id="@+id/applist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/appname"
              android:layout_width="200dp"
              android:layout_height="wrap_content"
              android:gravity="center_vertical"
              android:paddingTop="15dp"
              android:paddingLeft="10dp"
              android:paddingBottom="15dp"
              android:textStyle="bold"
              android:textSize="18sp"/>

<!--    <TextView
        android:id="@+id/app_package"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:visibility="invisible" />-->

    <ListView
        android:id="@+id/app"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:background="@drawable/checkbox_highlight_btn_check_holo_light"
        android:button="@drawable/checkbox_highlight_btn_check_holo_light"
        android:layout_height="wrap_content"
        android:scaleX="1.19"
        android:scaleY="1.19"
        android:focusable="false"
        android:layout_marginRight="20dp"
        android:layout_marginTop="12dp"
        android:focusableInTouchMode="false"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

How would I go about achieving this for my situation?

对于我的处境,我该如何实现这个目标呢?

5 个解决方案

#1


3  

intead of using this

使用这个,而不是

appname.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/raleway-regular.otf"));

try this

试试这个

 Typeface fond = Typeface.createFromAsset(context.getAssets(), "YOYR FONT FROM asset folder");
    appname.setTypeface(fond);

#2


2  

It's not that bad.

这不是那么糟糕。

holder.apkName.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/raleway-regular.otf"));

Hahaha I had the same problem. Took me a couple tries to figure this out on my custom adapter too. ;)

哈哈哈,我也有同样的问题。我带了几个人试着在我的自定义适配器上弄明白这一点。,)

#3


1  

Just define a custom TextView class like this:

像这样定义一个自定义TextView类:

public class AryanTextView extends TextView {

public AryanTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public AryanTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public AryanTextView(Context context) {
    super(context);
    init();
}

private void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            "font/YourFont.ttf");
    setTypeface(tf);
    setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
}
}

and then use this class (with its package address) in your xml instead of :

然后在xml中使用这个类(及其包地址),而不是:

<com.example.uicomponents.AryanTextView
 ..../>

#4


0  

just change condition only ::-



  if (convertView == null) {
            convertView = inflater.inflate(R.layout.installed_apps, null);
            holder = new ViewHolder();

            holder.apkName = (TextView) convertView.findViewById(R.id.appname);
            holder.ck1= (CheckBox)convertView.findViewById(R.id.checkBox1);
            holder.packageName = (TextView) convertView.findViewById(R.id.app_package);
            convertView.setTag(holder);


         holder.apkName.appname.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/raleway-         regular.otf"));
            //holder.ck1.setTag(packageList.get(position));
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

#5


0  

If you want to set typeface check this base adapter :

如果您想要设置字体,请检查此基础适配器:

public class AdapterListCitiesMarkets extends BaseAdapter {

private Context mContext;
private ArrayList<String> mMarketName;

public AdapterListCitiesMarkets(Context context, ArrayList<String> MarketName) {
    mContext = context;
    mMarketName = MarketName;
}

@Override
public int getCount() {
    return mMarketName.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    convertView = G.layoutInflater.inflate(R.layout.list_lay_cities_markets, null);

    TextView txtCityMarketName = (TextView) convertView.findViewById(R.id.txtCityMarketName);
    TextView txtCityMarketDistance = (TextView) convertView.findViewById(R.id.txtCityMarketDistance);
    txtCityMarketName.setTypeface(G.IranSans);
    txtCityMarketDistance.setTypeface(G.IranSans);

    txtCityMarketName.setText("فروشگاه : " + mMarketName.get(position));

    notifyDataSetChanged();
    return convertView;
}

}

#1


3  

intead of using this

使用这个,而不是

appname.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/raleway-regular.otf"));

try this

试试这个

 Typeface fond = Typeface.createFromAsset(context.getAssets(), "YOYR FONT FROM asset folder");
    appname.setTypeface(fond);

#2


2  

It's not that bad.

这不是那么糟糕。

holder.apkName.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/raleway-regular.otf"));

Hahaha I had the same problem. Took me a couple tries to figure this out on my custom adapter too. ;)

哈哈哈,我也有同样的问题。我带了几个人试着在我的自定义适配器上弄明白这一点。,)

#3


1  

Just define a custom TextView class like this:

像这样定义一个自定义TextView类:

public class AryanTextView extends TextView {

public AryanTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public AryanTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public AryanTextView(Context context) {
    super(context);
    init();
}

private void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            "font/YourFont.ttf");
    setTypeface(tf);
    setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
}
}

and then use this class (with its package address) in your xml instead of :

然后在xml中使用这个类(及其包地址),而不是:

<com.example.uicomponents.AryanTextView
 ..../>

#4


0  

just change condition only ::-



  if (convertView == null) {
            convertView = inflater.inflate(R.layout.installed_apps, null);
            holder = new ViewHolder();

            holder.apkName = (TextView) convertView.findViewById(R.id.appname);
            holder.ck1= (CheckBox)convertView.findViewById(R.id.checkBox1);
            holder.packageName = (TextView) convertView.findViewById(R.id.app_package);
            convertView.setTag(holder);


         holder.apkName.appname.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/raleway-         regular.otf"));
            //holder.ck1.setTag(packageList.get(position));
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

#5


0  

If you want to set typeface check this base adapter :

如果您想要设置字体,请检查此基础适配器:

public class AdapterListCitiesMarkets extends BaseAdapter {

private Context mContext;
private ArrayList<String> mMarketName;

public AdapterListCitiesMarkets(Context context, ArrayList<String> MarketName) {
    mContext = context;
    mMarketName = MarketName;
}

@Override
public int getCount() {
    return mMarketName.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    convertView = G.layoutInflater.inflate(R.layout.list_lay_cities_markets, null);

    TextView txtCityMarketName = (TextView) convertView.findViewById(R.id.txtCityMarketName);
    TextView txtCityMarketDistance = (TextView) convertView.findViewById(R.id.txtCityMarketDistance);
    txtCityMarketName.setTypeface(G.IranSans);
    txtCityMarketDistance.setTypeface(G.IranSans);

    txtCityMarketName.setText("فروشگاه : " + mMarketName.get(position));

    notifyDataSetChanged();
    return convertView;
}

}