簡體   English   中英

android更改listview字體和顏色

[英]android change listview font and color

我找到了很多不同的方法來實現這一點,但我不確定哪種方法最適合我的場景。

這是我用於列表視圖的 Java 代碼:

ListView lv;
lv = (ListView) findViewById(R.id.favList);

這是列表的 xml 代碼:

<ListView
        android:id="@+id/favList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="20dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@android:color/transparent" >
    </ListView>

對於文本視圖,我會添加:

final Typeface fontList = Typeface.createFromAsset(assets, "optima-extra-black.ttf");
lv.setTypeface(fontList);

但這不適用於列表視圖。 在這種情況下如何更改字體?

好的,我快到了...我需要訪問我的資產,但我不能從我的自定義適配器中訪問。 我嘗試使用final AssetManager assets = this.getAssets(); 但這不會讓我更進一步..

如何解決這個問題?

    class Myadapter extends BaseAdapter {

    LayoutInflater lif;
    ImageView sideArrow;
    TextView tv;


    public Myadapter(Context ctx) {
        lif = (LayoutInflater) ctx
                .getSystemService(LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {

        return favarets.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        if (convertView == null)
            vi = lif.inflate(R.layout.inflate, null);
        sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);

        tv = (TextView) vi.findViewById(R.id.textFav);
        tv.setText(favarets.get(position));
        final AssetManager assets = this.getAssets();
        final Typeface tvFont = Typeface.createFromAsset(assets, "OPTIMA.TTF");
        tv.setTypeface(tvFont);

        tv.setTextColor(Color.BLACK);

        return vi;

列表視圖本身不負責繪制項目,它使用適配器來創建列表項目。 此適配器創建一個視圖以在需要時顯示列表項。 要更改用於顯示列表項的字體,您必須更改適配器以返回具有新字體的視圖。 這可以在Adapter.getView方法中完成。 如果您當前正在使用標准的 Adapter 實現,則可能需要對其進行子類化(或完全替換它)。

我找到了解決方案:D

    public class Myadapter extends BaseAdapter {

    AssetManager assetManager = getAssets(); 

    LayoutInflater lif;
    ImageView sideArrow;
    TextView tv;


    public Myadapter(Context ctx) {
        lif = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);


    }

    @Override
    public int getCount() {

        return favarets.size();
    }

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

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


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        if (convertView == null)
            vi = lif.inflate(R.layout.inflate, null);
        sideArrow = (ImageView) vi.findViewById(R.id.imageViewsidemark);


        tv = (TextView) vi.findViewById(R.id.textFav);
        tv.setText(favarets.get(position));

        final Typeface tvFont = Typeface.createFromAsset(assetManager, "OPTIMA.TTF");
        tv.setTypeface(tvFont);
        tv.setTextColor(Color.BLACK);

        return vi;
    }

}

您需要創建一個custom adapter

檢查這個答案

然后也有一個custom xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView"
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
</LinearLayout>

然后將自定義適配器設置為您的listview

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList);
mListView.setAdapter(listAdapter);

簡單的,而不是使用SKD中的inbuild xml文件

`ArrayAdapter ad=new ArrayAdapter(GuestActivity.this,android.R.layout.simple_list_item_1,list);`

像這樣制作自己的xml布局文件-

`<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/YourStyle"
        android:gravity="center_vertical"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:minHeight="?android:attr/listPreferredItemHeightSmall" />`

並在適配器中使用它。

示例圖像:

格式化列表項

只需將其設置為您要充氣的 TextView,在適配器或 xml 布局中設置字體。

創建自定義文本視圖布局
第1步:
創建新布局,例如 listview_custom
第2步:
清除 listview_custom 中的所有代碼
第 3 步:
插入以下代碼

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
<!-- change background color -->    
android:background="@color/white"
    android:id="@android:id/text1"
<--!change text color-->
    android:textColor="@color/black"
<--!insert font--> 
    android:fontFamily="@font/iransansweb"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

並且很容易設置 listview Adapter

ArrayAdapter ad=new ArrayAdapter(MainActivity.this,R.layout.listview_custom,list);
listview.setAdapter(ad)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM