通过' if(position == x)'更改列表视图项目的文本颜色方法

时间:2014-09-11 20:02:59

标签: java android android-layout android-intent android-activity

是否可以使用' if position'以编程方式将列表视图项的文本颜色更改为十六进制颜色。方法?下面是我目前用于主要活动的代码,这里的if位置用于导航到其他活动,因此我想知道我的代码中需要添加或修改的内容。另外,如果我将应用程序的主题设置为Holo Dark,它是否总是这样说,那么购买我的应用程序的人是不是不可能篡改应用程序的主题?

public class MainActivity extends Activity {

ListView mainList;

String[] listContent = {
        "Line 1", "Line 2"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainList = (ListView)findViewById(R.id.main_listView);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listContent);
    mainList.setAdapter(adapter);

    mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            //When clicked, go to specific activity
            if(position == 0) {
                //code specific to first list item
                Intent myIntent = new Intent(view.getContext(), Line1Activity.class);
                startActivityForResult(myIntent, 0);
            }
            if(position == 1) {
                //code specific to second list item
                Intent myIntent = new Intent(view.getContext(), Line1Activity.class);
                startActivityForResult(myIntent, 0);
            }
    });
}

1 个答案:

答案 0 :(得分:1)

我会使用自定义ArrayAdapter根据getView中的位置设置颜色 像

这样的东西

<强> MainActivity.java

public class MainActivity extends Activity {

ListView mainList;
String[] listContent = {
        "Line 1", "Line 2", "Line 3", "Line 4"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainList = (ListView)findViewById(R.id.main_listView);
    MyColoringAdapter adapter = new MyColoringAdapter(this,listContent);
    mainList.setAdapter(adapter);

    mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            //When clicked, go to specific activity
            Toast.makeText(getApplicationContext(),"Clicked on list item "+position,Toast.LENGTH_SHORT).show();
            switch (position) {
                case 0:
                    //code specific to first list item
                    break;
                case 1:
                    //code specific to second list item
                    break;
            }
        }
    });
}

private class MyColoringAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MyColoringAdapter(Context context, String[] values) {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.list_textview);
        // Set text
        textView.setText(values[position]);
        // Set color depending on position
        int textColorId = R.color.black; // Default color
        switch (position%2) {
            case 0:
                // Even places
                textColorId = R.color.red; break;
            case 1:
                // Odd places
                textColorId = R.color.green; break;
        }
        textView.setTextColor(getResources().getColor(textColorId));
        return rowView;
    }
}
}

<强> list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/list_textview" />
</LinearLayout>

<强> colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
    <color name="black">#000000</color>
</resources>