首页 > 解决方案 > 无法显示阵列适配器项目它采用背景颜色

问题描述

我使列表项包括 2TextView和 1 ImageView,但所有项目都以相同的背景颜色显示,尽管我尝试强制更改数组适配器中的颜色但没有更改activity_main.xml代码是:

ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawSelectorOnTop="true"/>

列表item.xml

<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="@dimen/list_item_height"
    android:background="@color/tan_background"
    android:minHeight="@dimen/list_item_height">

    <ImageView
        android:id="@+id/image"
        android:layout_width="@dimen/list_item_height"
        android:layout_height="@dimen/list_item_height" />

    <RelativeLayout
        android:id="@+id/text_container"
        android:layout_width="match_parent"
        android:layout_height="@dimen/list_item_height"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/image"
        android:orientation="vertical"
        android:paddingLeft="16dp">

        <TextView
            android:id="@+id/miwok_text_view"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:layout_weight="1"
            android:gravity="bottom"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white"
            android:textStyle="bold"
            tools:text="lutti" />

        <TextView
            android:id="@+id/default_text_view"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:layout_below="@id/miwok_text_view"
            android:layout_weight="1"
            android:gravity="top"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white"
            tools:text="one" />


    </RelativeLayout>
</RelativeLayout>

主要活动代码是

public class MainActivity extends AppCompatActivity {

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

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);
 final ArrayList<Word> words = new ArrayList<Word>();
        words.add(new Word(R.string.category_alphabet, R.string.empty_string,
                R.drawable.alphabet, R.raw.sixty));
        words.add(new Word(R.string.category_numbers, R.string.empty_string,
                R.drawable.numbers, R.raw.seventy));
        words.add(new Word(R.string.category_colors, R.string.empty_string,
                R.drawable.colors, R.raw.eighty));
        words.add(new Word(R.string.category_family, R.string.empty_string,
                R.drawable.family, R.raw.ninty));
 FirstPageAdapter adapter = new FirstPageAdapter(this, words, R.color.try_under_color);

        // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
        // There should be a {@link ListView} with the view ID called list, which is declared in the
        // word_list.xml layout file.
        ListView listView = (ListView) findViewById(R.id.list);

        // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
        // {@link ListView} will display list items for each {@link Word} in the list.

        listView.setAdapter(adapter);
 listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Word word = words.get(i);
                if( word.getDefaultTranslationId()==R.string.category_alphabet){


                    Intent alphabetsIntent = new Intent(MainActivity.this, AlphabetsActivity.class);

                    // Start the new activity
                    startActivity(alphabetsIntent);

                }
                else if( word.getDefaultTranslationId()==R.string.category_numbers){

                    Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);

                    // Start the new activity
                    startActivity(numbersIntent);

                }

            }
        });


    }



}

我不知道如何显示列表项的内容

标签: android

解决方案


我通过添加 View view = listItemView.getRootView(); 解决了这个问题;view.setBackgroundColor(颜色); 但是出现了新问题,其中没有出现文本视图如何显示文本视图的内容


推荐阅读