【问题标题】:How to set RelativeLayout layout params in code not in xml?如何在代码中而不是在 xml 中设置 RelativeLayout 布局参数?
【发布时间】:2018-11-17 09:17:44
【问题描述】:

例如我想在屏幕上添加 3 个按钮:一个左对齐,一个居中,最后一个右对齐。

如何在代码中而不是在xml 中设置它们的布局?

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    只是一个基本的例子:

    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);
    

    如您所见,这是您必须做的:

    1. 创建一个RelativeLayout.LayoutParams 对象。
    2. 使用addRule(int)addRule(int, int) 设置规则。第一种方法用于添加不需要值的规则。
    3. 为视图设置参数(在本例中为每个按钮)。

    【讨论】:

    • 这里有几个问题。首先,我看不到您实际在哪里实例化 button1 或 button2。其次,动态声明的视图(ImageViews、Buttons 等)被实例化为 id 为 -1。 -1 的 id 不适用于规则。
    • 没有像 LayoutParams 这样的东西。基类实际上是ViewGroup.LayoutParams。如果您希望它更短,只需添加一个包含 RelativeLayout.LayoutParams 的导入。
    【解决方案2】:
        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);
    

    【讨论】:

      【解决方案3】:

      像这样..

       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);
      

      【讨论】:

        【解决方案4】:

        如果你创建了它,你从视图本身中提取布局参数怎么样。

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

        【讨论】:

          【解决方案5】:

          我希望下面的代码会有所帮助。它将创建一个 EditText 和一个登录按钮。两者相对放置。全部在 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);
                  }
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-02-17
            • 1970-01-01
            • 1970-01-01
            • 2013-06-24
            • 1970-01-01
            • 1970-01-01
            • 2020-09-17
            相关资源
            最近更新 更多