开发者

Android: Changing background color and/or text size in realtime

I have several ListActivties set up in my Android Application. What I am trying to do is set it up so the user can select the background color, the font size, and the font color through a PreferenceActivity. I have gotten everything figured out regarding the Preference activity but I can not figure out how to grab the ListView in code.

One of my ListActivities is set up in the normal way but it is required to have the android id tag "android:id="@android:id/list"". Because of this I am unable to change it in real time. (I was trying to use findViewById(R.id.list);)

My other ListActivities use a different implementation of ArrayAdapter in order to change the layouts of each row. So in this case I have one XML file containing the ListView and another XML file defining the layout of the row. I need to be able to change the TextView in side the row XML file and the ListView in the other XML file. The reason it is set up this way is to dynamically decide what icon to put in each row and in the implementation of ArrayAdapter I had to use an inflator to get to the label ImageView. In this case I am not sure how I would use an inflator out side of that implementation.

The following is an example of what I was talking about with the second listView.

public class Routes extends ListActivity {
private String[] ROUTES;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ROUTES = getResources().getStringArray(R.array.routes);

    setContentView(R.layout.routes);
    setListAdapter(new IconicAdapter());

}

class IconicAdapter extends ArrayAdapter<String> {


    IconicAdapter() {
        super(Routes.this, R.layout.row, R.id.label, ROUTES);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.label);

        label.setText(ROUTES[position]);

        ImageView icon = (ImageView) row.findViewById(R.id.icon);
        ShapeDrawable mDrawable;

        int x = 0;
        int y = 0;
        int width = 50;
        int height = 50;

        float[] outerR = new float[] { 12, 12, 12, 12, 12, 12, 12, 12 };

        mDrawable = new ShapeDrawable(new RoundRectShape(outerR, null, null));
        mDrawable.setBounds(x, y, x + width, y+height);
        mDrawable.setPadding(25,25,25,25);



        switch(position){

        case 0:
            mDrawable.getPaint().setColor(0xffff0000);      //Red
            break;
        c开发者_如何学Pythonase 1:
            mDrawable.getPaint().setColor(0xffff0000);      //Red
            break;
        case 2:
            mDrawable.getPaint().setColor(0xff00c000);      //Green
            break;
        case 3:
            mDrawable.getPaint().setColor(0xff00c000);      //Green
            break;
        case 4:
            mDrawable.getPaint().setColor(0xff0000ff);      //Blue
            break;
        case 5:
            mDrawable.getPaint().setColor(0xff0000ff);      //Blue
            break;
        }

        icon.setBackgroundDrawable(mDrawable);
        return(row);
    }


   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

  <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="7px"
android:paddingLeft="6px">

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="17px"
    android:textSize="28sp"
/>

Any suggestions would be appreciated!


Because of this I am unable to change it in real time.

Call getListView() on your ListActivity.


With regard to your other ListViews, I don't understand what your question is. You inflate the rows when you need them (NOTE: not on every getView() call, please recycle your convertView). You adjust rows at that point to look how you wish, just like you are adjusting your ImageView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜