Android 애플리케이션에 여러 ListActivties가 설정되어 있습니다. 사용자가 배경색, 글꼴 크기 및 글꼴 색을 PreferenceActivity를 통해 선택할 수 있도록 설정하려고합니다. 나는 Preference 액티비티에 관한 모든 것을 알아 냈다.하지만 코드에서 ListView를 잡는 방법을 알 수 없다.Android : 실시간으로 배경색 및/또는 텍스트 크기 변경
내 ListActivities 중 하나는 정상적인 방법으로 설정되지만 안드로이드 ID 태그 "android : id ="@ android : id/list ""가 있어야합니다. 이 때문에 나는 그것을 실시간으로 바꿀 수 없다. (나는 findViewById (R.id.list);를 사용하려고 시도했다;)
다른 ListActivities는 각 행의 레이아웃을 변경하기 위해 다른 ArrayAdapter 구현을 사용합니다. 따라서이 경우 ListView와 행의 레이아웃을 정의하는 다른 XML 파일을 포함하는 하나의 XML 파일이 있습니다. 행 XML 파일과 다른 XML 파일의 ListView에서 TextView를 변경할 수 있어야합니다. 이 방법으로 설정 한 이유는 동적으로 각 행에 넣을 아이콘을 결정하는 것이고 ArrayAdapter 구현시 Infragator를 사용하여 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"
/>
의견을 보내주십시오.
나는 getListView() 메소드에 대해 잊어 버렸고 나는 뭔가 효과가 있는지 찾아 내려고 미쳤다고 생각했다. 또한 getView() 호출을 변경하여 대부분 다른 부분을 해결했습니다. 내 convertView를 재활용하면 무슨 뜻인지 잘 모르겠다. 정교한 케어? 오, 그런데, 나는 정말 너의 책을 즐겼다. 그들은 거대한 도움이되었습니다. – tgai
@Tarmon : getView() 호출은 매번 새로운 행을 부 풀린다. 'convertView' 매개 변수를 통해 이전 행을 재활용하면 성능이 상당히 향상됩니다. 이 내용은 _Body Coder 's Guide to Android Development_의 "Listing With Fancy"장에서 다룰 수 있으며 http://commonsware.com/Android/excerpt.pdf – CommonsWare
에서 무료로 발췌 할 수도 있습니다. 감각. 그걸 정리 해줘서 고마워. – tgai