我面临 列表视图的问题,当 列表视图聚焦时,我想单独更改列表项中文本视图的文本颜色, 并且我想在右上角的列表项中显示图像。
以下代码我使用过。
在主类的 Xml 中
<ListView
android:id="@+id/listViewMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@null"
android:divider="@color/titlebar_divider"
android:dividerHeight="1dp"
android:listSelector="@android:color/transparent" >
</ListView>
在适配器 xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/imgMenuImages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:contentDescription="@null"
android:src="@drawable/my_page" />
<TextView
android:id="@+id/txtMenuNames"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:singleLine="true"
android:text="@string/mypage"
android:textColor="@color/menu_textcolor"
android:textStyle="bold" />
<ImageView
android:id="@+id/imgFocusImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"
android:duplicateParentState="true"
android:focusable="true"
android:focusableInTouchMode="false"
android:src="@drawable/arrow"
android:visibility="gone" />
</LinearLayout>
在适配器 JavaCode 中。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = mLayoutInflater.inflate(R.layout.menu_list_row, null);
TextView mTextView = (TextView)vi.findViewById(R.id.txtMenuNames);
mImageView = (ImageView)vi.findViewById(R.id.imgMenuImages);
imgFocusImage = (ImageView)vi.findViewById(R.id.imgFocusImage);
imgFocusImage.setFocusableInTouchMode(false);
mImageView.setImageResource(mThumbIds[position]);
mTextView.setText(mIndicators[position]);
mTextView.setOnFocusChangeListener(new OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus)
{
v.setBackgroundColor(hasFocus ? Color.GRAY : Color.BLACK);
}
});
imgFocusImage.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
v.setVisibility(View.VISIBLE);
}
});
return vi;
}
聚焦时,我可以改变Listitem
in的颜色ListView
,但是当它被按下时它会起作用。
在 Selector.xml 中
<item android:color="#FCBC04" android:state_focused="true"/>
<!-- focused -->
<item android:color="#FCBC04" android:state_focused="true" android:state_pressed="true"/>
<!-- focused and pressed -->
<item android:color="#FCBC04" android:state_pressed="true"/>
<!-- pressed -->
<item android:color="#D8D8D8"/>
<!-- default -->
谢谢你!!