在Focus上更改listview的背景图像

时间:2011-08-04 10:32:04

标签: android listview

enter image description here

我的代码是:

ListContacts.java

public class ListContacts extends ListActivity {
 @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("List Contacts : onCreate : ");

Cursor cursor = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null,
                ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1", null,
                "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");

        startManagingCursor(cursor);

ImageCursorAdapter .java

public class ImageCursorAdapter extends SimpleCursorAdapter implements SectionIndexer{

public View getView(int pos, View inView, ViewGroup parent) {



        View v = inView;
        String phoneNumber = null;
        String contactID = null;

        // Associate the xml file for each row with the view
        if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.contact_listview, null);

        }
        this.c.moveToPosition(pos);


  /**
         * Get the strings with the name and number of the person that the
         * current row
         */

        //
        String lName = this.c.getString(this.c
            .getColumnIndex(EMPLOYEE_NAME))


}



    }

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent" android:paddingLeft="5dp" android:paddingRight="5dp"
    android:layout_height="fill_parent" android:background="@drawable/listview_bac">


      <LinearLayout android:layout_width="fill_parent"
       android:layout_height="fill_parent" 
        android:layout_marginRight="4dp"       
         android:orientation="horizontal">

  <ImageView
        android:id="@+id/callimage" 
        android:layout_width="36dp"
        android:layout_height="49dp"
        android:clickable="true" 
        android:background="@android:color/transparent" 
         android:paddingRight="4dp"  
         android:src="@drawable/phone">
        </ImageView> 

     <LinearLayout android:layout_width="fill_parent"
      android:layout_height="wrap_content" 
       android:layout_gravity="center"     
         android:orientation="horizontal" >



         <TextView android:id="@+id/contact_name"
                android:textColor="@color/black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:textAppearance="?android:attr/textAppearanceLarge" 

                        />


            <ImageView
        android:id="@+id/favimage" 
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="top|right"
        android:background="@android:color/transparent" 
        android:src="@drawable/favorites" android:visibility="gone">
        </ImageView>




       <!--  <TextView android:id="@+id/contact_number"
                android:textColor="@color/black"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="2dp"
                android:textAppearance="?android:attr/textAppearanceSmall"

        /> -->

        </LinearLayout>


        </LinearLayout>



</LinearLayout>

我需要更改焦点列表视图的textview的图像颜色

3 个答案:

答案 0 :(得分:5)

添加xml文件以管理LinearLayout的背景。

list_bg_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Active tab -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/list_selected_bgimage" />
    <!-- Inactive tab -->
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@drawable/list_default_bgimage" />
    <!-- Pressed tab -->
    <item android:state_pressed="true" android:drawable="@drawable/list_selected_bgimage" />
    <!-- Selected tab (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/list_selected_bgimage" />
</selector> 

要管理文本颜色,请将文本颜色设置为另一个xml。

text_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:color="@android:color/black"/>
    <item android:state_focused="true" android:color="@android:color/black"/>
    <item android:state_pressed="true" android:color="@android:color/black"/>
    <item android:color="@android:color/white"/>

</selector>

答案 1 :(得分:1)

中的

if(position == focusedPosition) {
    textView.setColor(context.getResources().getColor(R.color.color_for_focused_item);
} else {
    textView.setColor(context.getResources().getColor(R.color.color_for_normal_item);
}

答案 2 :(得分:1)

试试这个

    listViewContactList.setOnFocusChangeListener(new OnFocusChangeListener(){

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
    v.setBackgroundColor(Color.BLACK);

   ImageView img=(ImageView)v.findViewById(R.id.callimage);
   img.setImageResource(R.drawable.yourimage);
   TextView tv=(TextView)v.findViewById(R.id.your_textview_id);
   tv.setBackgroundResource(R.drawable.your_back);      
    }

    });