Skip to content

Instantly share code, notes, and snippets.

@natanverdes
Created November 26, 2015 16:57
Show Gist options
  • Save natanverdes/202239b566a318bad2d9 to your computer and use it in GitHub Desktop.
Save natanverdes/202239b566a318bad2d9 to your computer and use it in GitHub Desktop.
Crear layouts en Android Studio mediante Java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creación de RelativeLayout
RelativeLayout rootView = new RelativeLayout(this);
// Creación de AnalogClock
AnalogClock analogClock1 = new AnalogClock(this);
analogClock1.setId(R.id.analogClock1);
// Añadimos LayoutParams al AnalogClock
RelativeLayout.LayoutParams analogClock1Params =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
analogClock1Params.addRule(RelativeLayout.CENTER_IN_PARENT);
analogClock1.setLayoutParams(analogClock1Params);
// Añadimos el AnalogClock al RelativeLayout
rootView.addView(analogClock1);
// Creación de TextView con sus LayoutParams
TextView textView1 = new TextView(this);
textView1.setId(R.id.textView1);
textView1.setTextSize(24);
textView1.setText(getResources().getString(R.string.hello_world));
// Añadimos LayoutParams al TextView
RelativeLayout.LayoutParams textView1Params =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textView1Params.addRule(RelativeLayout.ABOVE, R.id.analogClock1);
textView1Params.addRule(RelativeLayout.CENTER_HORIZONTAL);
textView1.setLayoutParams(textView1Params);
// Añadimos el TextView al LayoutParams
rootView.addView(textView1);
// Mostramos en la vista el RelativeLayout
setContentView(rootView);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment