当更改每个item点击后出现不同的颜色的方法;

<style name="Widget.AbsListView"> <item name="android:listSelector">@drawable/my_selector</item> </style>

 

my_selector在drawable

<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2008 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@color/transparent" /> <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. --> <item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_disabled" /> <item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_background_disabled" /> <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" /> <item android:state_focused="true" android:drawable="@drawable/list_selector_background_focus" /> </selector>

 

我想在代码中实现

public View getView(final int position, View convertView, ViewGroup parent) { int colorPos = position % colors.length; ... convertView.setBackgroundColor(colors[colorPos]); return convertView; }

 

可是没有成功 是不是没有刷新呢 画面呢

根据上面都没有成功,我看了好多有的成功有的没有成功不知道怎么弄的 就连第一种也没成功

color.xml

<resources>
<drawable name="list_normal">#96FFFFFF</drawable>
<drawable name="list_active">#66000000</drawable>
<drawable name="list_pressed">#CA000000</drawable>
</resources>

listview_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="@drawable/list_normal" />
<item android:state_pressed="true"
android:drawable="@drawable/list_pressed" />
<item android:state_focused="true"
android:drawable="@drawable/list_active" />
</selector>

还有的呢使用

然后

On ListView, set android:listSelector="@android:color/transparent".

************************************************************************

本来这个问题就留在这里的,不过那天一个网友一直向实现而不能实现,我就和他一起讨论很久试了很多方法,最后发现无论怎么样都是系统的颜色,后来我发现我们用的是listAvtivity,根本没有使用xml中的listActivty,所以我就把那些样式用在了主题当中结果可行 如下面的附件。

如果想按照以前上面的那些方法,那必须不能用listActivity本身的activity,或者直接用activity从里面找到lisView

总之你想用上面的方法 就是***上面的方法,我想你必须用你自己的listVIew而不是系统的。这只是我的猜想没有去实现嘿嘿。

最后又通过修改getView 证明也是可一的。

public class StyledListItems extends ListActivity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.list);        setListAdapter(new StyledListItemAdapter(this));    }            private class StyledListItemAdapter extends BaseAdapter {        public StyledListItemAdapter(Context context) {            mContext = context;        }        public int getCount() {            return mTitles.length;        }        public Object getItem(int position) {            return position;        }        public long getItemId(int position) {            return position;        }        public View getView(int position, View convertView, ViewGroup parent) {            if (convertView == null) {                sv = new StyledItemView(mContext, mTitles[position]);            } else {                sv = (StyledItemView) convertView;            }            sv.setContent(mTitles[position]);            return sv;        }        private Context mContext;                private String[] mTitles =         {                "lorem dipsum",                   "lorem dipsum",                "lorem dipsum",                       "lorem dipsum",                "lorem dipsum",                "lorem dipsum",                  "lorem dipsum",                "lorem dipsum"        };            }        private class StyledItemView extends LinearLayout {        private LayoutInflater mInflater;public StyledItemView(Context context, String title) {            super(context);            this.setOrientation(VERTICAL);            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            LinearLayout layout = (LinearLayout)mInflater.inflate(R.layout.list_item, null);            mTitle = (TextView)layout.findViewById(R.id.txt_item);            addView(layout);        }        public void setContent(String title) {            mTitle.setText(title);        }        private TextView mTitle;    }        StyledItemView sv;    }

list——tem

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/div_list_item"android:background="@drawable/list_item_style"android:layout_width="fill_parent"    android:layout_height="fill_parent" ><TextView android:id="@+id/txt_item"android:layout_width="fill_parent"android:textSize="14sp"    android:layout_height="20dip" />       </LinearLayout>

android:background="@drawable/list_item_style"
就是要变化的背景

The items in ListView is TextView, as below:

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/phone_numbers_row_text_tv"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/list_view_selector"
android:gravity="center"
/>

The "list_view_selector" is as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@drawable/number_select_bg"/>
<item
android:state_pressed="true"
android:drawable="@drawable/number_select_bg"/>
<item
android:state_focused="true"
android:drawable="@drawable/number_select_bg"/>
<item
android:drawable="@drawable/number_bg"/>
</selector>

更多相关文章

  1. Nginx系列教程(六)| 手把手教你搭建 LNMP 架构并部署天空网络电影
  2. android接收短信(SmsMessage.createFromPdu((byte[])obj)不推荐使
  3. android学习——android架构
  4. android实现Uri获取真实路径转换成File的方法
  5. Android(安卓)新的锁屏接口的实现
  6. Android(安卓)Studio与eclipse常用快捷键对比
  7. Android(安卓)Listview分组特效:滑动分组标题当前固定,并随内容滑
  8. 如何构建Android(安卓)Sync Provider :Part1
  9. android onGestureListener的方法

随机推荐

  1. Android中activity切换动画的两种实现(附
  2. android组件化方案、二维码扫码、Kotlin
  3. Android线程优先级设置方法
  4. 急需人才
  5. Android(安卓)studio 新建项目后报错:Coul
  6. Android(安卓)AsyncTask
  7. 在Android程序中使用全局变量
  8. Android中Bundle的使用示例
  9. Android核心分析(21)----Android应用框架之
  10. Android入门教程(四)之------Android工程