Android 更改导航抽屉中的项目
Android change items in Navigation Drawer
我想更改抽屉式导航布局中 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.access0(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.access0(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)
您对 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);
}
}
});
我想更改抽屉式导航布局中 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.access0(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.access0(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)
您对 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);
}
}
});