如何在ListView中的某些项目中添加字体和颜色

时间:2016-02-24 03:42:44

标签: java android listview fonts

我想知道如何将任何颜色和字体添加到任何ListView项目

这是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#E0E0E0"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ccb.lldm.lldmhimnario.Cantos">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:elevation="25dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

<SearchView
    android:layout_width="match_parent"
    android:id="@+id/searchView"
    android:queryHint="Busque Canto..."
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">
</SearchView>

<ListView
    android:layout_width="match_parent"
    android:id="@+id/listView"
    android:layout_height="wrap_content"
    android:layout_below="@+id/searchView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true">

</ListView>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:layout_alignTop="@+id/listView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />


  </RelativeLayout>

这是我的Java:

public class Cantos extends AppCompatActivity {

ListView lv;
SearchView sv;
String[] cantos={"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare",
        "5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven",
        "11: Alabad A Dios" };
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cantos);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle("Cantos");


    lv = (ListView) findViewById(R.id.listView);
    sv = (SearchView) findViewById(R.id.searchView);

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, cantos);
    lv.setAdapter(adapter);
    lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            switch (position) {
                case 0:
                    Intent i = new Intent(Cantos.this, cUNO.class);
                    startActivity(i);
                break;
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String text) {

            adapter.getFilter().filter(text);

            return false;
        }
    });
}
}

我是一名新的Android开发人员,我正在日复一日地学习。对不起,如果太笨拙了!谢谢。

3 个答案:

答案 0 :(得分:1)

尝试以这种方式实现它

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, cantos);

    adapter = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_list_item_1, cantos) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView text = (TextView) super.getView(position,
                    convertView, parent);
            if (cantos[position].equals("2: A Cristo Quiero"))
                text.setTextColor(Color.BLACK);
            else
                text.setTextColor(Color.BLUE);

            return text;
        }
    };

另外看这个example这是使用list的正确而有效的方法。

答案 1 :(得分:1)

为list创建自己的布局而不是android.R.layout.simple_list_item_1 使用TextView进行布局并设置颜色并将文本化为所需内容 user in adapter = new ArrayAdapter(this,R.layout.yourlayoutname,cantos);

答案 2 :(得分:-2)

您可以自己编写项目布局文件,而不是使用来自android sdk的资源。然后,您将能够编辑所需的颜色和字体。