更改Listview中的字体而不使用额外的适配器类

时间:2014-10-24 18:40:27

标签: java android android-listview

我无法更改列表视图中的行字体。我没有使用额外的适配器类,静态填充行。我想知道如何更改行的字体。您可以在下面找到我的代码。

public class Program extends Activity {

    private String[] gunler =
            {"Studio 1", "Studio 2", "Spinning", "Cross Fit"};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getActionBar();
        setContentView(R.layout.activity_program);

        //(A) adımı
        ListView listemiz=(ListView) findViewById(R.id.listView1);

        //(B) adımı
        ArrayAdapter<String> veriAdaptoru=new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, android.R.id.text1, gunler);

        final Typeface font = Typeface.createFromAsset(getAssets(),
                "futura-condensed-medium.ttf");

        //(C) adımı
        listemiz.setAdapter(veriAdaptoru);
        listemiz.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                Intent intent = new Intent(Program.this, ProgramDetail.class);
                int keyIdentifer = -1;
                intent.putExtra("kacinciGun", new  Integer(position+1).toString());
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        return super.onOptionsItemSelected(item);
    }

}

和布局文件:

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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您可以使用自定义TextView来设置字体。

public class CustomTextView extends TextView {

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

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

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

    public void init(Context context) {
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "your font name here");
        setTypeface(typeface);
    }
}

然后使用这个新的textview

创建一个新的布局
<com.your.package.CustomTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

然后在您的活动中创建适配器,如此

ArrayAdapter<String> veriAdaptoru = new ArrayAdapter<String>
            (this, R.layout.some_layout, R.id.text, gunler);