如何更改列表视图项目的字体颜色或大小

时间:2018-08-19 18:29:06

标签: java android listview

我正在寻找解决方案,希望将字体颜色更改为红色,并将大小更改为所需的列表视图项目大小。  我已经尝试过使用adaper,但我正在解决多个错误。

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        app:title="Space Time Alarm"
        android:layout_height="56dp"
        android:background="#30e410"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <ListView
        android:id="@+id/listView_Alarms"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="?android:attr/actionBarSize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

  


</android.support.constraint.ConstraintLayout>

Mainactivity文件是

public class MainActivity extends AppCompatActivity {
    public static boolean havePermission;

    private ListView listView_Alarms;
    private FloatingActionButton button_NewAlarm;
    private Toolbar toolbar;

    private DatabaseManager databaseManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("SPACESTOREALARM", "CREATING");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //setting up the toolbar and support
        toolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(toolbar);

        listView_Alarms = (ListView) findViewById(R.id.listView_Alarms);
        SpaceTimeAlarmManager.getInstance().initializeSpaceTimeAlarmManager(this);
        databaseManager = DatabaseManager.getInstance(this);
        databaseManager.initialize(this, listView_Alarms);
        listView_Alarms.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                SpaceTimeAlarm alarm = (SpaceTimeAlarm) listView_Alarms.getItemAtPosition(position);
                createOrEditAlarm(alarm);
            }
        });
   
    }

   
}

此处的列表视图包含警报列表,但由于字体和背景颜色相同,因此无法看到该列表。因此,我需要帮助来更改上述列表视图项目的字体颜色和大小。

谢谢。

2 个答案:

答案 0 :(得分:1)

我建议创建一个自定义ListView

  

检查this以创建自定义ListView

使用自定义ListView,您可以选择更多自定义选项。

答案 1 :(得分:0)

您可以设置适配器并更改背景颜色和项目大小,如下所示:

lv = findViewById(R.id.listView);
lv.setAdaptater(adapter);
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, list) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the current item from ListView
            View view = super.getView(position, convertView, parent);

            // Set the text color of TextView (ListView Item)
            TextView tv = view.findViewById(android.R.id.text1);
            // Generate ListView Item using TextView
            tv.setTextColor(Color.parseColor("#efefef"));
            tv.setTextSize(20);
            tv.setAllCaps(true);

                // Set a background color for ListView
                view.setBackgroundColor(Color.parseColor("#00afd8"));

            return view;
        }
    };
    lv.setAdapter(adapter);

您甚至可以替换背景颜色。