【问题标题】:How to change all color textview dynamically如何动态更改所有颜色的文本视图
【发布时间】:2016-02-16 06:28:17
【问题描述】:

我有代码,如果点击哪一个,我的 textview 颜色会改变,但它只会改变 1 行.. 我想改变所有列表项的颜色 enter image description here

这是我的截图 这是我的代码 我不知道这段代码有什么问题,

PS:即使我没有按下按钮,有时如果我滚动列表视图,颜色会自行改变

    public void colorToggle(View view) {
    int[] attrs = {android.R.attr.popupBackground};
    TypedArray ta = obtainStyledAttributes(R.style.MyApp_PopupMenu, attrs);
    final LinearLayout propLayout = (LinearLayout) findViewById(R.id.leot);
    ListView listView = (ListView) findViewById(android.R.id.list);
    TextView textView = (TextView) findViewById(R.id.wilayah);
    switch (view.getId()) {
        case R.id.blueButton: {
            int holoBlue = getResources().getColor(R.color.holo_blue_light);
            mFab.setColor(holoBlue);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoBlue));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_blue_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoBlue));
            listView.setDividerHeight(1);
            textView.setTextColor(holoBlue);
            break;
        }
        case R.id.purpleButton: {
            int holoPurple = getResources().getColor(R.color.holo_purple);
            mFab.setColor(holoPurple);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoPurple));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_purple);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoPurple));
            listView.setDividerHeight(1);
            textView.setTextColor(holoPurple);
            break;
        }
        case R.id.greenButton: {
            int holoGreen = getResources().getColor(R.color.holo_green_light);
            mFab.setColor(holoGreen);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoGreen));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_green_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoGreen));
            listView.setDividerHeight(1);
            textView.setTextColor(holoGreen);
            break;
        }
        case R.id.orangeButton: {
            int holoOrange = getResources().getColor(R.color.holo_orange_light);
            mFab.setColor(holoOrange);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoOrange));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_orange_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoOrange));
            listView.setDividerHeight(1);
            textView.setTextColor(holoOrange);
            break;
        }
        case R.id.redButton: {
            int holoRed = getResources().getColor(R.color.holo_red_light);
            mFab.setColor(holoRed);
            getActionBar().setBackgroundDrawable(new ColorDrawable(holoRed));
            mFab.setDrawable(getResources().getDrawable(R.drawable.ic_popup_sync_6));
            int popupBackground = ta.getColor(0, R.color.holo_red_light);
            Log.i("Retrieved textColor as hex:", Integer.toHexString(popupBackground));
            propLayout.setVisibility(View.GONE);
            listView.setDivider(new ColorDrawable(holoRed));
            listView.setDividerHeight(1);
            textView.setTextColor(holoRed);
            break;
        }
    }
    ta.recycle();    
}

【问题讨论】:

    标签: android listview


    【解决方案1】:

    换色后尝试调用adapter'snotifyDataSetChanged()。它应该为每个项目重新绘制整个 listview 并使用新颜色。

    【讨论】:

      【解决方案2】:

      如果我滚动 ListView,颜色会自行改变

      当您滚动时,ListView 中隐藏的项目将被回收或重新使用。

      详细解释见this

      您使用ListView 的方式似乎不正确。我建议您使用带有模型的适配器并将此适配器分配给ListView

      请参阅this 以使用自定义适配器实现 ListView。

      我想更改所有列表项的颜色

      首先实现自定义适配器。更改颜色时,只需遍历模型列表并更改每个项目的颜色。然后调用适配器的notifyDataSetChanged()

      希望这将帮助您解决问题。

      【讨论】:

        【解决方案3】:

        希望它能帮助你实现你的输出!

        ThirdActivity.java(包括适配器类)

        public class ThirdActivity extends Activity {
        
            View view_red, view_blue;
        
            ListView lst_data;
        
            int mySelectedColor;
        
            MyListAdapter myListAdapter;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.demo_3_list);
        
                lst_data = (ListView) findViewById(R.id.lst_data);
        
                // Statically taken two views for color selection
                view_blue = findViewById(R.id.view_blue);
                view_red = findViewById(R.id.view_red);
        
                mySelectedColor = ContextCompat.getColor(this, android.R.color.holo_red_dark);
        
                myListAdapter = new MyListAdapter(this, mySelectedColor);
                lst_data.setAdapter(myListAdapter);
        
                view_blue.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
        
                        if (myListAdapter == null) {
        
                            myListAdapter = new MyListAdapter(ThirdActivity.this, ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_blue_dark));
                            lst_data.setAdapter(myListAdapter);
        
                            return;
                        }
        
                        mySelectedColor = ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_blue_dark);
        
                        myListAdapter.notifyDataSetChanged();
        
                    }
                });
        
                view_red.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
        
                        if (myListAdapter == null) {
        
                            myListAdapter = new MyListAdapter(ThirdActivity.this, ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_red_dark));
                            lst_data.setAdapter(myListAdapter);
        
                            return;
                        }
        
                        mySelectedColor = ContextCompat.getColor(ThirdActivity.this, android.R.color.holo_red_dark);
        
                        myListAdapter.notifyDataSetChanged();
        
                    }
                });
        
        
            }
        
        
            public class MyListAdapter extends BaseAdapter {
        
        
                private int selectedMyColor;
                private Context context;
                private LayoutInflater mLayoutInflater;
        
                public MyListAdapter(Context contetx, int mySelectedColor) {
        
                    this.context = contetx;
        
                    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
                    this.selectedMyColor = mySelectedColor;
        
                }
        
                @Override
                public int getCount() {
                    return 20;
                }
        
                @Override
                public Object getItem(int position) {
                    return null;
                }
        
                @Override
                public long getItemId(int position) {
                    return 0;
                }
        
                @Override
                public View getView(int position, View view, ViewGroup parent) {
                    ViewHolder holder = null;
        
                    if (view == null) {
        
                        //The view is not a recycled one: we have to inflate
                        view = mLayoutInflater.inflate(R.layout.demo_3_row_layout, parent, false);
        
                        holder = new ViewHolder();
        
                        holder.txt_title = (TextView) view.findViewById(R.id.txt_title);
        
        
                        view.setTag(holder);
        
                    } else {
        
                        holder = (ViewHolder) view.getTag();
                    }
        
                    holder.txt_title.setTextColor(mySelectedColor);
        
                    return view;
                }
        
        
                class ViewHolder {
        
                    TextView txt_title;
        
                }
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-20
          • 2010-12-06
          相关资源
          最近更新 更多