android - 无法显示阵列适配器项目它采用背景颜色
问题描述
我使列表项包括 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);
}
}
});
}
}
我不知道如何显示列表项的内容
解决方案
我通过添加 View view = listItemView.getRootView(); 解决了这个问题;view.setBackgroundColor(颜色); 但是出现了新问题,其中没有出现文本视图如何显示文本视图的内容
推荐阅读
- scala - 将 List 的每个元素传递给 Scala 中的函数
- r - 当出现问题时,为什么 RStudio 不清除其全局环境?
- javascript - 我对 JavaScript Feature 有疑问——destructuring_assignment
- java - 编译器检测到 Eclipse 引用库 jar,但在运行时未检测到
- google-maps - 当我在单击标记时使用自定义弹出窗口时,我无法通过动画谷歌相机来放大相机
- c# - 如何执行嵌套?SQL中的请求?
- microsoft-graph-api - 列出地点 Microsoft Graph API
- api - REST API 设计和构建 URL
- google-sheets - 根据从当前日期确定的开始日期和结束日期显示即将到来的到期日期
- flutter - Flutter中如何通过多个类读取数据?