我想从导航抽屉布局中更改6个项目的文本样式。所以我尝试了:
for(int i = 0; i < 6; i++){
TextView tx = (TextView) mDrawerList.getAdapter().getView(i, null, null).findViewById(R.id.title);
tx.setTypeface(semibold);
tx.setTextSize(20);
}
但没有改变。 就像在我的onItemClick方法中一样,我得到了项目的视图,所以很容易在那里做到:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView tv = (TextView) view.findViewById(R.id.textView_title);
tv.setTypeface(semibold);
tv.setTextSize(20);
}
如何更改onItemClick方法之外的项目样式,如何引用该项目的视图?
编辑: 将getView更改为getChildAt后我得到了此异常
02-12 20:16:51.736 8133-8133/example.de.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: example.de.example, PID: 8133
java.lang.RuntimeException: Unable to start activity ComponentInfo{example.de.example/example.de.example.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at example.de.example.MainActivity.onCreate(MainActivity.java:155)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
at android.app.ActivityThread.access$800(ActivityThread.java:
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:
at android.os.Handler.dispatchMessage(Handler.java:
at android.os.Looper.loop(Looper.java:
at android.app.ActivityThread.main(ActivityThread.
at java.lang.reflect.Method.invokeNative(Native at java.lang.reflect.Method.invoke(Method.java:
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您对getView
的这些来电会创建ListView
实际上从未使用过的新观看次数。
您需要通过ListView
,而不是通过它的适配器来获取视图:
mDrawerList.post(new Runnable() {
@Override
public void run() {
// Loop all children and set the TextView's typeface
for(int i=0; i<mDrawerList.getChildCount(); i++){
View child = mDrawerList.getChildAt(i);
TextView tx = (TextView) child.findViewById(R.id.title);
if (tx == null) {
Log.e("TAG", "TextView at " + i + " not found!");
continue;
}
tx.setTypeface(semibold);
tx.setTextSize(20);
}
}
});