【发布时间】: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"
/>
任何建议将不胜感激!
【问题讨论】: