【问题标题】:Android: Changing background color and/or text size in realtimeAndroid:实时更改背景颜色和/或文本大小
【发布时间】:2010-06-04 23:33:26
【问题描述】:

我在我的 Android 应用程序中设置了几个 ListActivties。我要做的是设置它,以便用户可以通过 PreferenceActivity 选择背景颜色、字体大小和字体颜色。我已经弄清楚了有关 Preference 活动的一切,但我不知道如何在代码中获取 ListView。

我的 ListActivities 之一是以正常方式设置的,但它需要具有 android id 标签“android:id="@android:id/list"”。因此,我无法实时更改它。 (我试图使用 findViewById(R.id.list);)

我的其他 ListActivities 使用不同的 ArrayAdapter 实现来更改每一行的布局。所以在这种情况下,我有一个包含 ListView 的 XML 文件和另一个定义行布局的 XML 文件。我需要能够更改行 XML 文件中的 TextView 和其他 XML 文件中的 ListView。以这种方式设置的原因是动态决定在每一行中放置什么图标,并且在 ArrayAdapter 的实现中,我必须使用充气器来获取标签 ImageView。在这种情况下,我不确定如何在该实现之外使用充气机。

以下是我所说的第二个 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;
        case 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"
/>

任何建议将不胜感激!

【问题讨论】:

    标签: android listview


    【解决方案1】:

    因此我无法更改 它是实时的。

    在您的ListActivity 上致电getListView()


    关于你的另一个ListViews,我不明白你的问题是什么。您可以在需要时对行进行充气(注意:并非每次调用 getView(),请回收您的 convertView)。您此时调整行以查看您希望的方式,就像您调整 ImageView 一样。

    【讨论】:

    • 我想我已经忘记了 getListView() 方法,我正疯狂地试图找到有效的方法。通过在我的 getView() 调用中实现更改,我还大部分解决了我的问题的另一部分。话虽如此,我不确定回收我的 convertView 是什么意思。需要详细说明吗?哦,顺便说一句,我真的很喜欢你的书。他们提供了巨大的帮助。
    • @Tarmon:您的getView() 调用每次都会增加一个新行。通过回收通过 convertView 参数提供给您的先前行,您将获得更好的性能。这在 The Busy Coder's Guide to Android Development 的“Getting Fancy with Lists”一章中有介绍,也可在此处免费摘录:commonsware.com/Android/excerpt.pdf
    • 这更有意义。感谢您为我解决这个问题。
    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多