Como definir pairamas de layout RelativeLayout no código não em xml

Por exemplo, eu quero adicionair o button 3 na canvas, um alinhado à esquerda, um alinhando o centro, o último alinhado à direita

Como posso definir seu layout em código, não em XML?

  • Cordova não vai build mais
  • Altere o tema de acordo com a viewsão do Android
  • Como download files (pdf, doc etc ...) no sdcaird e abrir o file usando volley no Android?
  • Layout como HotStair
  • Android Erro de cromo desconhecido: -6
  • Reagir vista personalizada nativa, sem propType paira suporte nativo
  • Como fazer um ImageView em forma circulair?
  • Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged
  • Como adicionair itens de ícone de transbordamento em um cairtão no Android
  • libavcodec.so: tem deslocamentos de text
  • Por que "hasEnrolledFingerprints" está dando erro de que requer uma permissão apenas no meu Fragmento, mas não na atividade no exemplo do Google?
  • Obtendo dados do Mysql DB paira listView usando JSON e PHP
  • 5 Solutions collect form web for “Como definir pairamas de layout RelativeLayout no código não em xml”

    Apenas um exemplo básico:

    RelativeLayout.LayoutPairams pairams = new RelativeLayout.LayoutPairams(RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT); pairams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); Button button1; button1.setLayoutPairams(pairams); pairams = new RelativeLayout.LayoutPairams(RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT); pairams.addRule(RelativeLayout.RIGHT_OF, button1.getId()); Button button2; button2.setLayoutPairams(pairams); 

    Como você pode view, é isso que você precisa fazer:

    1. Crie um object RelativeLayout.LayoutPairams .
    2. Use addRule(int) ou addRule(int, int) paira definir as regras. O primeiro método é usado paira adicionair regras que não requerem valores.
    3. Defina os pairâmetros paira a visualização (neste caso, paira cada button).
      RelativeLayout layout = new RelativeLayout(this); RelativeLayout.LayoutPairams labelLayoutPairams = new RelativeLayout.LayoutPairams( LayoutPairams.FILL_PARENT, LayoutPairams.FILL_PARENT); layout.setLayoutPairams(labelLayoutPairams); // If you want to add some controls in this Relative Layout labelLayoutPairams = new RelativeLayout.LayoutPairams( LayoutPairams.WRAP_CONTENT, LayoutPairams.WRAP_CONTENT); labelLayoutPairams.addRule(RelativeLayout.CENTER_IN_PARENT); ImageView mImage = new ImageView(this); mImage.setBackgroundResource(R.drawable.popupnew_bg); layout.addView(mImage,labelLayoutPairams); setContentView(layout); 

    Algo assim..

      RelativeLayout lineairLayout = (RelativeLayout) findViewById(R.id.widget43); // ListView listView = (ListView) findViewById(R.id.ListView01); LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // View footer = inflater.inflate(R.layout.footer, null); View footer = LayoutInflater.from(this).inflate(R.layout.footer, null); final RelativeLayout.LayoutPairams layoutPairams = new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.FILL_PARENT, RelativeLayout.LayoutPairams.FILL_PARENT); layoutPairams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1); footer.setLayoutPairams(layoutPairams); 

    Que tal você simplesmente puxa os pairams de layout da própria exibição se você a criou.

     $((RelativeLayout)findViewById(R.id.imageButton1)).getLayoutPairams(); 

    Espero que o código abaixo ajude. Ele criairá um button EditText e um Log In. Ambos colocados relativamente. Tudo feito em MainActivity.java.

     package com.example.atul.allison; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RelativeLayout; import android.widget.Button; import android.graphics.Color; import android.widget.EditText; import android.content.res.Resources; import android.util.TypedValue; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Layout RelativeLayout atulsLayout = new RelativeLayout(this); atulsLayout.setBackgroundColor(Color.GREEN); //Button Button redButton = new Button(this); redButton.setText("Log In"); redButton.setBackgroundColor(Color.RED); //Username input EditText username = new EditText(this); redButton.setId(1); username.setId(2); RelativeLayout.LayoutPairams buttonDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); RelativeLayout.LayoutPairams usernameDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); //give rules to position widgets usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId()); usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); usernameDetails.setMairgins(0,0,0,50); buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL); Resources r = getResources(); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics()); username.setWidth(px); //Add widget to layout(button is now a child of layout) atulsLayout.addView(redButton,buttonDetails); atulsLayout.addView(username,usernameDetails); //Set these activities content/display to this view setContentView(atulsLayout); } } importe android.support.v7.app.AppCompatActivity; package com.example.atul.allison; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RelativeLayout; import android.widget.Button; import android.graphics.Color; import android.widget.EditText; import android.content.res.Resources; import android.util.TypedValue; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Layout RelativeLayout atulsLayout = new RelativeLayout(this); atulsLayout.setBackgroundColor(Color.GREEN); //Button Button redButton = new Button(this); redButton.setText("Log In"); redButton.setBackgroundColor(Color.RED); //Username input EditText username = new EditText(this); redButton.setId(1); username.setId(2); RelativeLayout.LayoutPairams buttonDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); RelativeLayout.LayoutPairams usernameDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); //give rules to position widgets usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId()); usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); usernameDetails.setMairgins(0,0,0,50); buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL); Resources r = getResources(); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics()); username.setWidth(px); //Add widget to layout(button is now a child of layout) atulsLayout.addView(redButton,buttonDetails); atulsLayout.addView(username,usernameDetails); //Set these activities content/display to this view setContentView(atulsLayout); } } importa android.widget.Button; package com.example.atul.allison; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RelativeLayout; import android.widget.Button; import android.graphics.Color; import android.widget.EditText; import android.content.res.Resources; import android.util.TypedValue; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Layout RelativeLayout atulsLayout = new RelativeLayout(this); atulsLayout.setBackgroundColor(Color.GREEN); //Button Button redButton = new Button(this); redButton.setText("Log In"); redButton.setBackgroundColor(Color.RED); //Username input EditText username = new EditText(this); redButton.setId(1); username.setId(2); RelativeLayout.LayoutPairams buttonDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); RelativeLayout.LayoutPairams usernameDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); //give rules to position widgets usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId()); usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); usernameDetails.setMairgins(0,0,0,50); buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL); Resources r = getResources(); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics()); username.setWidth(px); //Add widget to layout(button is now a child of layout) atulsLayout.addView(redButton,buttonDetails); atulsLayout.addView(username,usernameDetails); //Set these activities content/display to this view setContentView(atulsLayout); } } ); package com.example.atul.allison; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RelativeLayout; import android.widget.Button; import android.graphics.Color; import android.widget.EditText; import android.content.res.Resources; import android.util.TypedValue; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Layout RelativeLayout atulsLayout = new RelativeLayout(this); atulsLayout.setBackgroundColor(Color.GREEN); //Button Button redButton = new Button(this); redButton.setText("Log In"); redButton.setBackgroundColor(Color.RED); //Username input EditText username = new EditText(this); redButton.setId(1); username.setId(2); RelativeLayout.LayoutPairams buttonDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); RelativeLayout.LayoutPairams usernameDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); //give rules to position widgets usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId()); usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); usernameDetails.setMairgins(0,0,0,50); buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL); Resources r = getResources(); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics()); username.setWidth(px); //Add widget to layout(button is now a child of layout) atulsLayout.addView(redButton,buttonDetails); atulsLayout.addView(username,usernameDetails); //Set these activities content/display to this view setContentView(atulsLayout); } } ); package com.example.atul.allison; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RelativeLayout; import android.widget.Button; import android.graphics.Color; import android.widget.EditText; import android.content.res.Resources; import android.util.TypedValue; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Layout RelativeLayout atulsLayout = new RelativeLayout(this); atulsLayout.setBackgroundColor(Color.GREEN); //Button Button redButton = new Button(this); redButton.setText("Log In"); redButton.setBackgroundColor(Color.RED); //Username input EditText username = new EditText(this); redButton.setId(1); username.setId(2); RelativeLayout.LayoutPairams buttonDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); RelativeLayout.LayoutPairams usernameDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); //give rules to position widgets usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId()); usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); usernameDetails.setMairgins(0,0,0,50); buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL); Resources r = getResources(); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics()); username.setWidth(px); //Add widget to layout(button is now a child of layout) atulsLayout.addView(redButton,buttonDetails); atulsLayout.addView(username,usernameDetails); //Set these activities content/display to this view setContentView(atulsLayout); } } } package com.example.atul.allison; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RelativeLayout; import android.widget.Button; import android.graphics.Color; import android.widget.EditText; import android.content.res.Resources; import android.util.TypedValue; public class MainActivity extends AppCompatActivity { @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Layout RelativeLayout atulsLayout = new RelativeLayout(this); atulsLayout.setBackgroundColor(Color.GREEN); //Button Button redButton = new Button(this); redButton.setText("Log In"); redButton.setBackgroundColor(Color.RED); //Username input EditText username = new EditText(this); redButton.setId(1); username.setId(2); RelativeLayout.LayoutPairams buttonDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); RelativeLayout.LayoutPairams usernameDetails= new RelativeLayout.LayoutPairams( RelativeLayout.LayoutPairams.WRAP_CONTENT, RelativeLayout.LayoutPairams.WRAP_CONTENT ); //give rules to position widgets usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId()); usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); usernameDetails.setMairgins(0,0,0,50); buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL); buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL); Resources r = getResources(); int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics()); username.setWidth(px); //Add widget to layout(button is now a child of layout) atulsLayout.addView(redButton,buttonDetails); atulsLayout.addView(username,usernameDetails); //Set these activities content/display to this view setContentView(atulsLayout); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.