动态添加的视图未显示

时间:2018-03-01 00:52:08

标签: android android-activity

在我的主要活动中,我调用以下内容来显示新活动:

User::with('jobRole')->get(...);

这会在我的Intent intent = new Intent( MainActivity.this, FindActivity.class ); startActivity( intent ); 中调用我的onCreate()代码:

FindActivity

我的活动声明如下:

  @Override
  protected void onCreate( Bundle savedInstanceState )
  {
    Utils.LogDebugf( "FindActivity:: onCreate()\n" );
    super.onCreate( savedInstanceState );

    mActivity = this;

    mBaseLinearLayout = new LinearLayout( mActivity );
    mBaseLinearLayout.setLayoutParams( new LinearLayout.LayoutParams( ContextActivity.screenWidth, ContextActivity.screenHeight ) );
    mBaseLinearLayout.setOrientation( LinearLayout.VERTICAL );
    mBaseLinearLayout.setBackgroundColor( Color.TRANSPARENT );

    mEditText = new EditText( mActivity );

    mEditText.setWidth( ContextActivity.screenWidth );
    mEditText.setHeight( mResultLabelHeight );
    mEditText.setBackgroundColor( Color.DKGRAY );
    mEditText.setGravity( Gravity.CENTER );
    mEditText.setText( "" );
    mEditText.setTextColor( Color.YELLOW );
    mEditText.setTextSize( 30 );
    mEditText.setSingleLine();
    mEditText.setLayoutParams( new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ) );

    mEditText.setOnClickListener( buttonListener );
    mEditText.addTextChangedListener( mTextWatcher );

    mListView = new ListView( mActivity );
    mListView.setBackgroundColor( Color.WHITE );
    mListView.setLayoutParams( new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1 ) );

    mBaseLinearLayout.addView( mEditText );
    mBaseLinearLayout.addView( mListView );
}

我遇到的问题是我的观点/控件都没有出现。我有一个黑色的场景。我错过了什么?

1 个答案:

答案 0 :(得分:2)

即使您没有为Activity的内容使用XML布局,您仍然需要为动态创建的{{1}调用setContentView()要附加到View的s。例如:

Activity

只需确保将根setContentView(mBaseLinearLayout); 传递给那里,否则您将获得View