我正在尝试更改我TextView
活动中使用的ListView
的文本大小。但它在运行时显示错误。
我的活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings();
// Check Full Screen
if (Fullscreen_Sett == true) {
// hide statusbar of Android
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
// Content View
setContentView(R.layout.tutoriallist_layout);
// Backlight ON/OFF
if (Backlight_Sett == true) {
getWindow()
.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
// ActionBar
ActionBar ActionBar = getSupportActionBar();
ActionBar.setTitle("Tutorials");
ActionBar.setDisplayHomeAsUpEnabled(true);
ActionBar.setHomeButtonEnabled(true);
// load the animation
animfade_in = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
animfade_out = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);
// *********load the animation
// ***********Set TextSize Of List
TextView tv = (TextView)findViewById(R.id.tutoriallist_text_ID);
tv.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
// Strings
final String[] TutorialArray = getResources().getStringArray(
R.array.Tutorial_Array);
// List View Import
lv = (ListView) findViewById(R.id.tutorial_listview);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.tutoriallist_text,
R.id.tutoriallist_text_ID, TutorialArray);
lv.setAdapter(adapter);
if (Animation_Sett == true) {
// Check Animation
lv.startAnimation(animfade_in);
}
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (arg2 == 0) {
Toast.makeText(getApplicationContext(), "Tutorial",
Toast.LENGTH_SHORT).show();
}
}
});
}
我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tutoriallist_text_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/tutorialliststyle"
android:gravity="center"
android:lines="2"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="20sp"
android:typeface="monospace" />
</LinearLayout>
LOG CAT:
08-01 11:10:34.744: E/AndroidRuntime(3826): FATAL EXCEPTION: main
08-01 11:10:34.744: E/AndroidRuntime(3826): android.content.res.Resources$NotFoundException: Resource ID #0x7f060038 type #0x12 is not valid
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.content.res.Resources.getLayout(Resources.java:853)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.ListView.onMeasure(ListView.java:1158)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.View.measure(View.java:15513)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.View.measure(View.java:15513)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.View.measure(View.java:15513)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.View.measure(View.java:15513)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-01 11:10:34.744: E/AndroidRuntime(3826): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.View.measure(View.java:15513)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.Choreographer.doFrame(Choreographer.java:532)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.os.Handler.handleCallback(Handler.java:725)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.os.Looper.loop(Looper.java:137)
08-01 11:10:34.744: E/AndroidRuntime(3826): at android.app.ActivityThread.main(ActivityThread.java:5039)
08-01 11:10:34.744: E/AndroidRuntime(3826): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 11:10:34.744: E/AndroidRuntime(3826): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 11:10:34.744: E/AndroidRuntime(3826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 11:10:34.744: E/AndroidRuntime(3826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 11:10:34.744: E/AndroidRuntime(3826): at dalvik.system.NativeStart.main(Native Method)
注意:我已尝试过tv.settextsize()等。但它仍然存在的错误......
答案 0 :(得分:1)
这可以帮到你:
Buddy我认为您遇到此问题是因为您使用setContentView(R.layout.tutoriallist_layout);
作为主要布局
尝试实例化一个名为TextView
的变量R.layout.tutoriallist_text
: TextView tv = (TextView)findViewById(R.id.tutoriallist_text_ID);
tv.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
<强>像:强>
TextView
删除这些行并尝试在其layout itself
中设置// ***********Set TextSize Of List
TextView tv = (TextView)findViewById(R.id.tutoriallist_text_ID);
tv.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Small);
的属性...
修改强>
评论这两行并再试一次:
public class CustomAdapter extends ArrayAdapter<String>{
Context context;
修改1:
这是您应该如何创建自定义适配器:
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
}
else
{
row = counterView;
}
TextView tv = (TextView)row.findViewById(R.id.tutoriallist_text_ID);
tv.setTextAppearance(context, android.R.style.TextAppearance_Small);
tv.setTextSize(specify size that you want);
tv.setText(data.getIndex(position));
return row;
}
}
int layoutResourceId;
ArrayList data = null;
// Adding items to listview
CustomAdapter adapter = new CustomAdapter(this, R.layout.tutoriallist_text, TutorialArray);
并将此行改为MyActivity:
{{1}}
答案 1 :(得分:0)
保留一个可以更改的类变量:
private flot textSize = 15;
更改适配器返回的视图:
adapter = new ArrayAdapter<String>(this, R.layout.tutoriallist_text,
R.id.tutoriallist_text_ID, TutorialArray){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView tv = v.findViewById(R.id.tutoriallist_text_ID);
tv.setTextSize(textSize);
return v;
}
};
使用以下方式更改文字大小
textSize = 20;
adapter.notifyDatsetChanged();