Comment définir les paramètres de mise en page RelativeLayout dans le code pas en xml
Par exemple je veux ajouter 3 bouton sur l'écran d'un aligner à gauche, centrer, dernier aligner à droite
Comment puis-je définir leur mise en page dans le code, pas en XML?
109
                
            
                demandé sur
                Cristian
                2011-03-04 10:51:52
            
        5 réponses
Juste un exemple de base:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1;
button1.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
Button button2;
button2.setLayoutParams(params);
Comme vous pouvez le voir, voici ce que vous devez faire:
-  créez un objet 
RelativeLayout.LayoutParams. - Utiliser 
addRule(int)ouaddRule(int, int)pour définir les règles. La première méthode est utilisée pour ajouter des règles qui ne nécessitent pas de valeurs. - définissez les paramètres sur la vue (dans ce cas, sur chaque bouton).
 
264
	                    
	                
	                    répondu
	                    Cristian
	                    2011-03-04 07:59:38
	                
	                RelativeLayout layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    layout.setLayoutParams(labelLayoutParams);
   // If you want to add some controls in this Relative Layout
    labelLayoutParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    labelLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    ImageView mImage = new ImageView(this);
    mImage.setBackgroundResource(R.drawable.popupnew_bg);        
    layout.addView(mImage,labelLayoutParams);
    setContentView(layout);
	                17
	                    
	                
	                    répondu
	                    Amit Thaper
	                    2013-01-31 01:39:01
	                
	            Quelque Chose comme ça..
 RelativeLayout linearLayout = (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.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.FILL_PARENT,
                        RelativeLayout.LayoutParams.FILL_PARENT);
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
footer.setLayoutParams(layoutParams);
	                6
	                    
	                
	                    répondu
	                    Rohit Mandiwal
	                    2011-03-04 08:01:43
	                
	            Que diriez-vous de simplement tirer les paramètres de mise en page de la vue elle-même si vous l'avez créé.
$((RelativeLayout)findViewById(R.id.imageButton1)).getLayoutParams();
	                4
	                    
	                
	                    répondu
	                    Chidi Michael Ekeocha
	                    2012-10-20 12:49:03
	                
	            J'espère que le code ci-dessous vous aidera. Il va créer un EditText et un bouton de connexion. Les deux placés relativement. Tout est fait dans 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 {
        @Override
        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.LayoutParams buttonDetails= new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
            RelativeLayout.LayoutParams usernameDetails= new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT
            );
            //give rules to position widgets
            usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId());
            usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
            usernameDetails.setMargins(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);
        }
    }
	                0
	                    
	                
	                    répondu
	                    Atul Chavan
	                    2016-10-16 07:11:51