更改列表视图对焦点的背景图片视图、背景图片、焦点、列表

2023-09-07 00:24:25 作者:山风灼疏影

我的code是:

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

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的颜色

I need to change the image, color of textview of the listview on focus

推荐答案

添加一个XML文件来管理的LinearLayout的背景。

Add an xml file to manage the background of the LinearLayout.

list_bg_selector.xml:

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。

To manage the text color, set the text color to another xml.

text_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>