0

我正在尝试更改我的活动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>

日志猫:

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() 和其他。但它仍然是相同的错误......

4

2 回答 2

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;
数组列表数据=空;

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);
于 2013-08-01T11:49:28.400 回答
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();
于 2013-08-01T11:34:12.327 回答