2013-08-01 75 views
0

我正在嘗試更改我的活動的ListView中使用的TextView的文本大小。但它在運行時顯示錯誤。Android:在運行時更改列表視圖的Textview大小

我的動態

@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

安置自己的堆棧跟蹤 – Tarun

+0

它顯示了哪些錯誤或異常?Logcat Plz –

+0

更新logcat/stacktrace –

回答

1

可以幫助您:

好友我想你是因爲你使用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); 

編輯1:

這是你應該如何創建一個自定義適配器:

public class CustomAdapter extends ArrayAdapter<String>{ 

    Context context; 

int layoutResourceId;
ArrayList data = null;

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; 


    } 

} 

並改變這一行你MyActivity:

// Adding items to listview 
    CustomAdapter adapter = new CustomAdapter(this, R.layout.tutoriallist_text, TutorialArray); 
+0

主要佈局tutoriallist_layout有列表視圖,並使用tutoriallist_text在列表視圖,即使用的TextView,tutoriallist_text_ID 我以前用這個方法仍然得到錯誤:( –

+0

你在'R.layout.tutoriallist_text'佈局的textview中使用'R.id.tutoriallist_text_ID',所以嘗試對這些行進行註釋,然後再次嘗試buddy .... –

+0

哪一行應該評論bro?Sorry,I am a a新手我不明白你 我想在運行時根據用戶更改文本大小 –

0

保持一個類變量,您可以更改:

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; 
     } 
}; 

更改文字大小有:通過適配器返回

private flot textSize = 15; 

阿爾特意見

textSize = 20; 
adapter.notifyDatsetChanged(); 
+0

它顯示在Eclipse的智能感知錯誤示值誤差兄弟@ SD –

相關問題