2015-01-10 3 views
0

MainActivity.java에서 레이아웃을 만들려고하지만 내 에뮬레이터가 레이아웃과 MainActivity.java에서 만든 버튼을 표시하지 않습니다. activity_main.xml에서 무언가를 변경해야합니까? 는 여기에 내가 코드를 테스트하지 않았다MainActivity.java에서 레이아웃을 만들 수 없습니다.

package com.example.pratt.myapplication; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.util.Log; 
import android.widget.RelativeLayout; 
import android.widget.Button; 
import android.graphics.Color; 


public class MainActivity extends ActionBarActivity { 

    private static final String TAG="My Message"; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     /* 
     */ 
     super.onCreate(savedInstanceState); 
     RelativeLayout myLayout=new RelativeLayout(this); 
     Button blue_button=new Button(this); 
     myLayout.setBackgroundColor(Color.RED); 
     myLayout.addView(blue_button); 
     blue_button.setText("Blue Button"); 
     Log.i(TAG,"onCreate"); 
     setContentView(myLayout); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

답변

0

, 코드입니다,하지만 난 당신이 예를 들어, 레이아웃의 폭과 높이를 들어, 레이아웃에 대한 몇 가지 LayoutParams을 추가하지 않았기 때문에 그것은 같아요. 고정 너비와 높이를 사용하지 않으려면 대신 LayoutParams.WRAP_CONTNT 또는 MATCH_PARENT을 사용할 수 있습니다.

0

@Override 전에 Button을 만들어야한다고 가정합니다.

private static final String TAG="My Message"; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     /* 
     */ 
     super.onCreate(savedInstanceState); 
     RelativeLayout myLayout=new RelativeLayout(this); 
     myLayout.setBackgroundColor(Color.RED); 
     Button blue_button=new Button(this); 
     blue_button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,  
     LayoutParams.WRAP_CONTENT) 
     myLayout.addView(blue_button); 
     blue_button.setText("Blue Button"); 
     Log.i(TAG,"onCreate"); 
     setContentView(myLayout); 
    } 

당신은 button의 크기를 지정하지 않았습니다. 따라서 0dp x 0dp 버튼을 만듭니다.

희망이 도움이됩니다.

+0

효과가 없습니다. –

관련 문제