0

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

这是我的代码

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


}

}

4

1 回答 1

0

我得到了这个问题。这是一个程序错误,由于计算错误,视图被添加到不同的页面。

于 2012-11-16T18:40:39.993 回答