У меня есть несколько ListActivties, настроенных в моем приложении Android. Я пытаюсь настроить его так, чтобы пользователь мог выбирать цвет фона, размер шрифта и цвет шрифта с помощью PreferenceActivity. Я понял все, что касается действия Preference, но я не могу понять, как получить ListView в коде.
Одна из моих функций ListActivities настроена обычным образом, но для нее требуется тег android id "android: id =" @ android: id / list "". Из-за этого я не могу изменить его в режиме реального времени. (Я пытался использовать findViewById (R.id.list);)
Мои другие ListActivities используют другую реализацию ArrayAdapter для изменения макетов каждой строки. Так что в этом случае у меня есть один XML-файл, содержащий ListView, и другой XML-файл, определяющий расположение строки. Мне нужно иметь возможность изменить TextView в стороне XML-файла строки и ListView в другом XML-файле. Причина, по которой он настроен таким образом, состоит в том, чтобы динамически решить, какой значок поместить в каждую строку, и в реализации 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"
/>
Любые предложения будут оценены!