LayoutInflater wordSpan

时间:2014-02-24 09:55:44

标签: java android html

我想在OnCreateView上设置wordtoSpan字符串。

我的代码:

package com.tachles;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class History_moadim_a extends Fragment {

    TextView horef;
    TextView kaiz;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);

        return rootView;

        horef = (TextView) findViewById(R.id.textView3);
        Spannable wordtoSpan = new SpannableString("hgh");
        horef.setText(wordtoSpan);



    }
}

Eclipse说我不能使用findViewById,所以我试图插入getView()但仍然是同样的问题。请告诉我我的错误

谢谢

2 个答案:

答案 0 :(得分:3)

如果history_moadim_a是包含textView3的布局,请将onCreateView()更改为

View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);

horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);

return rootView;

您想要在刚刚膨胀的布局中找到TextViewrootView),并在 end 处返回布局(而不是在方法的中间) )。

答案 1 :(得分:1)

使用

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class History_moadim_a extends Fragment {

    TextView horef;
    TextView kaiz;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);



        horef = (TextView) rootView.findViewById(R.id.textView3);
        Spannable wordtoSpan = new SpannableString("hgh");
        horef.setText(wordtoSpan);

 return rootView;

    }
}

我无法在完美的地方看到onCreateView上的return语句,所以请在onCreateView的末尾通过onCreateView

的末尾在onCreateView内部return rootView ;进行操作