【问题标题】:How can I design this layout programmatically?如何以编程方式设计此布局?
【发布时间】:2014-01-23 05:40:20
【问题描述】:

我有一个父线性布局。

我需要在屏幕底部放置三个水平对齐的按钮,而不是通过 XML,而是通过 Java 代码。
按钮 1 应位于屏幕左侧
按钮 2 应位于屏幕底部
按钮 3 应该在屏幕右侧

这是需要设计的布局:

我的理解是我必须向我的父布局添加一个相对布局。 还有一些按钮规则。

这是我尝试过的

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    l=(LinearLayout)findViewById(R.id.mainl);
    rl=new RelativeLayout(this);
    b1=new Button(this);
    b2=new Button(this);
    b3=new Button(this);
    b1.setText("Button 1");
    b2.setText("Button 2");
    b3.setText("Button 3");
    rl.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    rl.addView(b1);
    rl.addView(b2);
    rl.addView(b3);

    l.addView(rl);

}

而且按钮也没有出现在底部。它们出现在屏幕顶部。

Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    要将按钮添加到 RelativeLayout 运行时,您必须传递参数值。

    尝试在左侧放置按钮b1

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    rl.addView(b1, params);
    

    【讨论】:

      【解决方案2】:
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      
          params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
          rl.addView(b1, params);
      
      
          RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
          params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
          rl.addView(b3, params1);
      
          RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams
                  ( (int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
      
          params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
          rl.addView(b2, params2);
      
      
      
      LinearLayout.LayoutParams layoutParams =
                      (RelativeLayout.LayoutParams) txt1.getLayoutParams();
          layoutParams.addRule(LinearLayout.BOTTOM, 1);
      
      rl.setLayoutParams(layoutParams);
      

      【讨论】:

      • 它对我很有用。但按钮不在屏幕底部。他们来到屏幕的顶部。我如何将它们保持在底部。
      • 这里的txt1是什么?并且也没有识别出线性布局的底部
      【解决方案3】:

      试试这个

      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                  LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
          layoutParams.gravity = Gravity.BOTTOM;
          rl.setLayoutParams(layoutParams);
      
      
      
          RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      
          params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
      
          RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
          params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
      
          RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(
                  (int) LayoutParams.WRAP_CONTENT,
                  (int) LayoutParams.WRAP_CONTENT);
      
          params3.addRule(RelativeLayout.CENTER_HORIZONTAL);
      
          rl.addView(b1, params1);
          rl.addView(b2, params3);
          rl.addView(b3, params2);
      
          l.addView(rl);
      

      【讨论】:

      • 按钮不在屏幕底部。
      • 我认为您在 activity_main.xml 文件中使用线性布局高度作为 wrap_content。对吗?
      • 如果是,则将 layout_height 更改为 fill_parent。
      • 然后发布您的 activity_main.xml 文件
      【解决方案4】:

      试试这个: XML 文件:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="bottom"
          android:orientation="vertical"
          tools:context=".MainActivity" >
      
      </LinearLayout>
      

      在 JAVA 文件中:

       LinearLayout main = (LinearLayout) findViewById(R.id.main);
      
              RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                      android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
                      android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
              RelativeLayout ll = new RelativeLayout(getBaseContext());
      
              RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
                      android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                      android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
              params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
              RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
                      android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                      android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
              params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
      
              RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(
                      android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                      android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
              params3.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
      
              Button btn1 = new Button(getBaseContext());
              Button btn2 = new Button(getBaseContext());
              Button btn3 = new Button(getBaseContext());
      
              btn1.setText("One");
              btn2.setText("Two");
              btn3.setText("Three");
      
              ll.addView(btn1, params1);
              ll.addView(btn2, params2);
              ll.addView(btn3, params3);
      
              main.addView(ll, params);
      

      希望这会对你有所帮助。

      【讨论】:

      • 我希望通过 java 代码而不是通过 xml 创建相对布局
      • 所以你的父 LINEAR LAYOUT 在 XML 文件中。对吗??
      • 我已经更新了我的答案。请检查更新。它对我有用。
      • 它解决了我一半的问题。它工作得很好。但我还需要将这些按钮放在屏幕底部。你知道怎么做吗?
      【解决方案5】:

      我已经使用此代码以编程方式创建了布局:

      public class MainActivity extends Activity {
          private LinearLayout layout; 
          private RelativeLayout lay;
          private Button btn1, btn2, btn3;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              layout = (LinearLayout)findViewById(R.id.ff);
              lay = new RelativeLayout(this);
              RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
              layout_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
              lay.setLayoutParams(layout_params);
      
              //lay.setLayoutParams(layout_params);
              btn1 = new Button(this);
              btn1.setText("ButtonA");
              btn2 = new Button(this);
              btn2.setText("ButtonB");
              btn3 = new Button(this);
              btn3.setText("ButtonC");
      
              RelativeLayout.LayoutParams button_params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
              button_params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
              button_params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
              btn1.setLayoutParams(button_params1);
      
              RelativeLayout.LayoutParams button_params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
              button_params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
              button_params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
              btn2.setLayoutParams(button_params2);
      
              RelativeLayout.LayoutParams button_params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
              button_params3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
              button_params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
              btn3.setLayoutParams(button_params3);
      
              lay.addView(btn1);
              lay.addView(btn2);
              lay.addView(btn3);
              layout.addView(lay);
      
      
      
          }
      }
      

      【讨论】:

      • 我不能有父级相对布局。父布局应该只是线性的。我需要将相对布局添加到父线性布局。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多