如何以编程方式添加另一个文本?

时间:2013-09-23 06:15:15

标签: android android-layout

我在我的代码中设置了通知监听器。每当我收到通知时,我想在我的linear layout活动中放置一个文本视图,该视图最初没有任何内容  当第二个通知到达时,我想在上一个文本视图下面再添加一个文本视图。

public class InformationActivity extends Activity
{

    public static VideoInformationClass vidInfo = new VideoInformationClass() ;
    public static LinearLayout lv ;
    public static LayoutParams textViewParams;
    public static TextView tv ;
    public static TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7,tv8,tv9,tv10;
    static int fieldFrequency;
    static int numberOfFrameLines;
    static int numberOfVisibleLines;
    static int numberOfVisiblePixels;
    static int interlace;
    static int imageFormat;
    static int videoCoding;
    static int scanType;

    VideoPropertiesParams GetParams;

    VideoPropertiesParams VP;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.information_activity);

        Context context = getBaseContext();
        LinearLayout lv = new LinearLayout(this);
        lv.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
       // tv = new TextView(this);


    }

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    onFieldFrequencyChanged();
    onImageFormatChanged();
    onInterlacedChanged();
    /*vidInfo.RegisterVidPropertyListener();
    vidInfo.RegisterSignalAndPresenceListener();
    vidInfo.RegisterThreeDChangeListener();*/
    if (ControlUnit.flag) {
    GetParams = vidInfo.GetVideoProperty();
    Log.i("TvPlayerFunctionalTestApp","Get Video Property called");
    fieldFrequency = GetParams.fieldFrequency;
    numberOfFrameLines = GetParams.numberOfFrameLines;
    numberOfVisibleLines = GetParams.numberOfVisibleLines;
    numberOfVisiblePixels = GetParams.numberOfVisiblePixels;
    interlace = GetParams.interlace;
    imageFormat = GetParams.imageFormat;
    videoCoding = GetParams.videoCoding;
    scanType = GetParams.scanType;
    }

   /* tv.setText("Something");
    tv.setLayoutParams(textViewParams);
    lv.addView( tv );*/
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    /*vidInfo.UnRegisterSignalAndPresenceListener();
    vidInfo.UnRegisterThreeDChangeListener();
    vidInfo.UnRegisterVidPropertyListener();*/
}
public void onFieldFrequencyChanged(){

    String info = "On field frequency changed , value is " + vidInfo.ChangedFrequency;

    tv = new TextView(this);
    tv.setText(String.valueOf(info));
    LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(textViewParams);
    lv.addView(tv);
}
public void onImageFormatChanged() {

    String info = "On Image Format  changed , value is " + vidInfo.ChangedFormat;
    tv1 = new TextView(this);
    tv1.setText(String.valueOf(info));
    LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    tv1.setLayoutParams(textViewParams);
    lv.addView(tv1 );
}
public void onInterlacedChanged() {

    String info = "On Interlaced  changed , value is " + vidInfo.InterlaceChange;
    tv.setText(String.valueOf(info));
    LinearLayout.LayoutParams textView
Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(textViewParams);
    lv.addView(tv);
}

4 个答案:

答案 0 :(得分:3)

只需声明垂直方向的线性布局

       <LinearLayout
         android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

     LinearLayout lv = (LinearLayout)findViewById(R.id.linear);
      TextView tv = new TextView(this);

  tv.setLayoutParams(InformationActivity.textViewParams);
  tv.setText(String.valueOf(info));
  lv.addView(tv );//not InformationActivity.tv just write tv

它会自动在另一个

下面添加下一个视图

答案 1 :(得分:0)

确保LinearLayout的方向设置为垂直方向。然后再添加一个项目:

LinearLayout layout = // your layout
TextView textView = // new text view
layout.addView(textView);

答案 2 :(得分:0)

只需制作一个方向垂直的Linear Layout,然后为其添加TextView

答案 3 :(得分:0)

您需要来自主ViewGroup的对象(RelativeLayout,LinearLayout等),

TextView text = new TextView(this);     RelativeLayout r = (RelativeLayout)findViewById(Id);r.add(text);

这行代码会将一个新的textView对象添加到r viewGroup的末尾。