android TextView使用代码设置边距和背景

Android活动中….如何仅使用代码而不是XML进行以下操作?

<TextView android:background="#0000ff" android:layout_margin="2dip"/>

我还需要文本右对齐…

卡在上面一会儿=(如果有人可以帮忙

谢谢

更新

我尝试了下面的代码

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 
            RelativeLayout.LayoutParams.FILL_PARENT,                
            RelativeLayout.LayoutParams.FILL_PARENT);                
            //250);

    lp.setMargins(5,5,5,5);

而我做到了

row.addView(t1, lp);
row.addView(t2);
row.addView(t3);
row.addView(t4);

我也尝试了LinearLayout

但由于某种原因t1不再显示.

解决方法:

这是如何编写textView的代码:

    mTextView = new TextView(this);
    mTextView.setGravity(Gravity.CENTER_VERTICAL);       
    mTextView.setText(R.string.instructions);
    mTextView.setTextColor(0xFF000000);
    mTextView.setPadding(20, 8, 8, 20);

    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);

    mScroll = new ScrollView(this);
    mScroll.setScrollbarFadingEnabled(false);
    mTextPane = new RelativeLayout(this);
    mTextPane.setVisibility(View.GONE);
    //mScroll.setVisibility(View.GONE);

    mScroll.addView(mTextView);
    mTextPane.addView(mScroll);

    Resources res = getResources();
    //Drawable drawable = res.getDrawable(R.drawable.text_pane_feather2);
    Drawable drawable = res.getDrawable(R.drawable.text);
    mTextPane.setBackgroundDrawable(drawable);

    //RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 420, 420 );
    RelativeLayout.LayoutParams lp = 
        new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,
                //RelativeLayout.LayoutParams.WRAP_CONTENT);
                250);

    lp.setMargins(0,0,0,30);
    lp.addRule(RelativeLayout.CENTER_HORIZONTAL );
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    layout.addView(mTextPane, lp);
上一篇:android-自定义视图中的渐变文本颜色


下一篇:android-一般访问TextView