在AsyncTask中向RelativeLayout添加TextView不起作用

时间:2012-11-15 19:15:02

标签: android textview relativelayout

我将几个TextView添加到RelativeLayout。我已经扩展了RelativeLayout。我可以在构造函数定义中添加TextView。我可以看到viw上的项目。但是相同的代码,当我在AyncTask的onPostExecute中使用或使用post()方法时,没有看到TextView被添加到布局中。两个地方都执行相同的系数。我只是改变了上边距。

这是我的代码

TextView tv = new TextView(context);
    addView(tv);


    tv.setHeight(49);

    tv.setWidth(100);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.leftMargin = (int) 0;
    params.topMargin = (int) 600;
    tv.setLayoutParams(params);

================实施============================== ==

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        DateDetailsScrollView sc = new DateDetailsScrollView(container.getContext());

        new SyncInThread().execute(sc);
        return sc;
    }

================= AsyncTask ============================= ==

private static class SyncInThread extends AsyncTask<Object, Void, String> {

    private ArrayList<EVT> _mEvents = new ArrayList<EVT>();
    DateDetailsScrollView _lv = null;

    @Override
    protected String doInBackground(Object... cal) {
        _lv = (DateDetailsScrollView)cal[0];

        //The SQL cannot be shared here... Please forgive me.
        boolean f = cursor.moveToFirst();
        while(f) {
            EVT evt = new EVT();
            evt.loadFromCursor(cursor);
            f = cursor.moveToNext();
            _mEvents.add(evt);
            cc++;
        }
        return "";
    }

    @Override
    protected void onPostExecute(String result) {

        for(EVT ev : _EVT) {
            _lv.addEvent(ev);
        }

    }

}


public class DateDetailsScrollView extends ScrollView {
    private EventRelativeLayout _mEventLayout = null;

    public DateDetailsScrollView(Context context) {
        super(context);
        _mEventLayout = new EventRelativeLayout(context);
        ll.addView(_mEventLayout);
        LinearLayout ll = new LinearLayout(context);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        addView(ll);
    }


    public void addEvent(final VEVENT evt) {
            _mEventLayout.addEvent(evt);
            invalidate();
            this.refreshDrawableState();
    }
}



    public class EventRelativeLayout extends RelativeLayout {


public EventRelativeLayout(Context context) {
    //This works fine.
    super(context);
    TextView tv = new TextView(context);
    addView(tv);

    int[] colors = { 0xFF052d42, 0xFF096da0 };
    GradientDrawable drawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM, colors);

    tv.setBackgroundColor(0xFF290202);
    tv.setBackgroundDrawable(drawable);
    tv.setHeight(49);

    tv.setWidth(100);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.leftMargin = (int) 0;
    params.topMargin = (int) 100;
    tv.setLayoutParams(params);

}

public void addEvent(EVT evt) {



    TextView tv = new TextView(getContext());
    addView(tv);

    int[] colors = { 0xFF052d42, 0xFF096da0 };
    GradientDrawable drawable = new GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM, colors);

    tv.setBackgroundColor(0xFF290202);
    tv.setBackgroundDrawable(drawable);
    tv.setHeight(100);

    int dm = em -eh;
    int dh = eh - sh;
    if( dm < 0) {
        dm += 60;
        dh--;
    }

    tv.setWidth(100);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.leftMargin = 0;
    params.topMargin = 600;
    tv.setHeight(49);
    tv.setLayoutParams(params);


}

}

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题。这是一个程序错误,由于计算错误,将视角添加到另一页。