2

我有一些列表视图。这是项目视图的代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" android:background="@drawable/list_drawable_settings"
              android:layout_height="fill_parent" android:gravity="center_vertical" android:orientation="vertical">
    <LinearLayout
            android:id="@+id/section"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
        <com.vk.messenger.views.TextViewMyriadPro android:id="@+id/word"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"/>
     </LinearLayout>
    <RelativeLayout android:layout_width="fill_parent"
                   android:layout_height="wrap_content" android:gravity="center_vertical">
        <LinearLayout android:layout_height="wrap_content" android:layout_centerVertical="true" android:gravity="center_vertical" android:layout_width="fill_parent">
        <com.vk.messenger.views.RoundedConersImageView android:id="@+id/userpic"
                   android:layout_marginLeft="9dip" android:layout_marginRight="9dip"
                   android:layout_marginTop="6dip" android:layout_marginBottom="6dip"
                   android:layout_height="40dip"
                   android:layout_width="40dip"/>
        <com.vk.messenger.views.TextViewMyriadPro android:id="@+id/name"
                  android:layout_toRightOf="@id/userpic"
                  android:textColor="@color/friends_text_color"
                  android:textSize="15dip"
                  android:textStyle="bold"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"/>
        </LinearLayout>
        <ImageView android:id="@+id/online"
                   android:visibility="invisible"
                   android:layout_marginRight="9dip"
                   android:layout_centerVertical="true"
                   android:src="@drawable/online_list"
                   android:layout_height="wrap_content"
                   android:layout_width="wrap_content"
                   android:layout_alignParentRight="true"/>
    </RelativeLayout>
</LinearLayout>

当 listview 的元素聚焦时,主层的背景会发生变化。如何在主元素集中时更改 textview 元素的文本颜色?

PS对不起我的英语不好(

4

2 回答 2

0

这对于有状态的颜色非常简单:

创建“颜色列表”文件。例如res/color/font_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="@android:color/holo_red_light" />
    <item android:state_selected="true"
          android:color="@android:color/holo_blue_light" />
    <item
        android:color="@android:color/white" />
</selector>

然后在您的布局中:<TextView android:textColor="@color/font_color_selector" ... />

于 2014-07-01T05:38:27.837 回答
0

我这样做的想法是创建一个自定义数组适配器,其中包含一个表示 textview 元素颜色的整数变量。在getView自定义适配器的方法中,您将设置 textViews 的颜色。像这样的东西:

public class CustomAdapter extends
    BaseAdapter {

private String[] data; //Your data
private Context context;
    private int textViewColor = Color.BLACK; //The color of your textViews with a default value

public CustomAdapter(Context _context, String[] _data) {
    context = _context;
    data = _data;
}

    public void setColor(int _color) {
        textViewColor = color;
    }

    //Use a viewholder if you havent already for more efficient listView.
    static class ViewHolder {
    protected TextView textView;
}

@Override
public View getView(int position, View inView, ViewGroup parent) {

    View v = inView;
    ViewHolder viewHolder;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.your_list_view_item_layout, null);
        viewHolder = new ViewHolder();
        viewHolder.textView = (TextView) v.findViewById(R.id.your_textViewId);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }

          viewHolder.textview.setTextColor(textViewColor); //Set the color of your textViews
          //Do anything else you are supposed to...
   }

  ....
}

然后OnFocusChangeListener在您的 listView 上设置一个。在那里,设置你的 textViews 的新颜色。像这样的东西:

listView.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    CustomAdapter adapter = (CustomAdapter) listView.getAdapter();
                    adapter.setColor(Color.White); //set the new color of your textViews components
                    adapter.notifyDataSetChanged(); //Call this to update the listview

                }
            });

如果您只想更改 listView 中所选项目的 textView 的颜色,则需要getView()在适配器中的方法上将 onFocusChangedListener 添加到 listViewItem 中。像这样的东西:

@Override
public View getView(int position, View inView, ViewGroup parent) {

    View v = inView;
    ViewHolder viewHolder;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.your_list_view_item_layout, null);
        viewHolder = new ViewHolder();
        viewHolder.textView = (TextView) v.findViewById(R.id.your_textViewId);
        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }


          //set the onFocusChangedListener in the View item of the listView
          v.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                TextView textView = (TextView) v.findViewbyId(R.id.your_textViewId);
                if(hasFocus) {
                      textview.setTextColor(Color.BLUE);
                }
                else {
                      textview.setTextColor(Color.BLACK);
                }

            }
        });

          //Do anything else you are supposed to...
   }
于 2012-07-04T12:36:42.927 回答