【问题标题】:How to make two buttons on the same row in eclipse programmatically?如何以编程方式在eclipse中的同一行上制作两个按钮?
【发布时间】:2018-05-23 08:50:06
【问题描述】:
LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);

btnControl = new Button(this);
    btnControl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    btnControl.setPadding(0,40,0,40);//x,y,w,h
    btnControl.setBackgroundColor(Color.parseColor("#1C344E"));
    btnControl.setTextColor(Color.WHITE);
    setMargins(btnControl, 50, 50, 50, 50);

btnLogs = new Button(this);
        btnLogs.setLayoutParams(new LayoutParams(380, LayoutParams.WRAP_CONTENT));
        btnLogs.setPadding(0,40,0,40);//x,y,w,h
        btnLogs.setBackgroundColor(Color.parseColor("#1C344E"));
        btnLogs.setTextColor(Color.WHITE);
        setMargins(btnLogs, 50, 50, 50, 50);
        btnLogs.setId(5);
        btnLogs.setOnClickListener(this);

        btnTermLogs = new Button(this);
        btnTermLogs.setLayoutParams(new LayoutParams(380, LayoutParams.WRAP_CONTENT));
        btnTermLogs.setPadding(0,40,0,40);//x,y,w,h
        btnTermLogs.setBackgroundColor(Color.parseColor("#CC6675"));
        btnTermLogs.setTextColor(Color.WHITE);
        setMargins(btnLogs, 50, 50, 50, 50);
        btnTermLogs.setId(6);
        btnTermLogs.setOnClickListener(this);

btnLogout = new Button(this);
    btnLogout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    btnLogout.setPadding(0,40,0,40);//x,y,w,h
    btnLogout.setBackgroundColor(Color.parseColor("#CC6675"));
    btnLogout.setTextColor(Color.WHITE);

    table.addView(btnLogs);
        table.addView(btnTermLogs);

我希望两个按钮水平对齐。但是它只是垂直对齐自己,这是我不想要的。

我的按钮如下所示:

我想要的是这样的:

【问题讨论】:

  • @intelliJ Amiya,您给我的重复站点是所有按钮都在循环内。我还有另一个需要垂直对齐的按钮

标签: android eclipse


【解决方案1】:

试试下面这个函数。会有帮助的

public void addButtons(){
    LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);

    LinearLayout linearLayout1 = new LinearLayout(this);
    linearLayout1.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams LayoutParamsview = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    Button button1 = new Button(this);
    button1.setText("Btn 1");
    button1.setLayoutParams(LayoutParamsview);

    linearLayout1.addView(button1);
    table.addView(linearLayout1);

    //
    LinearLayout linearLayout2 = new LinearLayout(this);
    linearLayout2.setOrientation(LinearLayout.HORIZONTAL);

    Button button2 = new Button(this);
    button2.setText("Btn 2");
    button2.setLayoutParams(LayoutParamsview);

    Button button3 = new Button(this);
    button3.setText("Btn 3");
    button3.setLayoutParams(LayoutParamsview);

    linearLayout2.addView(button2);
    linearLayout2.addView(button3);
    table.addView(linearLayout2);

    //
    LinearLayout linearLayout3 = new LinearLayout(this);
    linearLayout3.setOrientation(LinearLayout.VERTICAL);

    Button button4 = new Button(this);
    button4.setText("Btn 4");
    button4.setLayoutParams(LayoutParamsview);

    linearLayout3.addView(button4);
    table.addView(linearLayout3);

}

【讨论】:

    【解决方案2】:

    水平线性布局支持从左到右的小部件方法,当 应用程序开发人员在线性布局中选择水平方向然后它 将自动开始将组件和小部件添加到左侧 添加它们后的右侧。所以这里是完整的一步一步 以编程方式创建水平线性布局的教程 安卓。

    编写这段代码

         //Creating LinearLayout.
         LinearLayout Horizontallinearlayout = new LinearLayout(this);
    
         //Setting up LinearLayout Orientation
         Horizontallinearlayout.setOrientation(LinearLayout.HORIZONTAL);
    
         LayoutParams Horizontallinearlayoutlayoutparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    
         setContentView(Horizontallinearlayout, Horizontallinearlayoutlayoutparams);
    
         LayoutParams LayoutParamsview = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
         //Creating textview .
         Button button1 = new Button(this);
         Button button2 = new Button(this);
    
         //Adding text to TextView.
         button1.setText("First Button");
         button2.setText("Second Button");
    
         button1.setLayoutParams(LayoutParamsview);
         button2.setLayoutParams(LayoutParamsview);
    
         //Adding textview to linear layout using Add View function.
         Horizontallinearlayout.addView(button1);
         Horizontallinearlayout.addView(button2);
    

    希望对你有帮助

    【讨论】:

    • @ulyk 谢谢。很乐意为您提供帮助。
    【解决方案3】:
    • 你应该使用setLayoutParams
    • 添加setOrientation(LinearLayout.HORIZONTAL);

    设置与此视图关联的布局参数。这些供应 此视图的父级的参数,指定它应该如何 安排的。 ViewGroup.LayoutParams 有很多子类,并且 这些对应于 ViewGroup 的不同子类,它们是 负责安排他们的孩子。

    代码结构

        LinearLayout table = (LinearLayout)findViewById(R.id.linearlayout);
        table.setOrientation(LinearLayout.HORIZONTAL);
    
        Button Btn1 = new Button(Activity.this);
        Btn1.setText("Button 1");
        LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        childParam.weight = 0.5f;
        Btn1.setLayoutParams(childParam);
    
        Button Btn2 = new Button(Activity.this);
        Btn2.setText("Button 2");
        LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        childParam2.weight = 0.5f;
        Btn2.setLayoutParams(childParam2);
    
        table.addView(Btn1);
        table.addView(Btn2);
    

    尊敬的 OP

    【讨论】:

    • 我更新了我的问题 :) 带圆圈的按钮是我唯一需要水平对齐的按钮
    • @ulyk 请检查我的答案。
    • 它使我的两个按钮垂直而不是水平对齐 :( 你给我的android-examples.com/… 网站是它的所有按钮都是水平对齐的。但是在我的情况下,我有两个垂直对齐的按钮还有另外两个按钮,我现在根据我的图像水平对齐。
    • 它会影响我的其他按钮:(如果我把 setOrientation(LinearLayout.HORIZONTAL);
    • 在我的图片中,我有按钮 0 和按钮 3,它们不应受到按钮 1 和 2 的更改的影响
    【解决方案4】:

    您可以创建另一个XML 文件,其中包含一个水平LinearLayout,然后将其膨胀并将两个按钮插入其中,然后将整个水平LinearLayout 添加到垂直LinearLayout:

    1. 创建一个XML 文件,例如horizontal_layout.xml:

      <LinearLayout
          android:id="@+id/horizontal_layout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
      
      // You will insert the buttons programmatically.
      
      </LinearLayout>
      
    2. 在您的 Activity 中使用此方法为 Layout 充气:

          LinearLayout verticalLinearLayout = rootView.findViewById(R.id.vertical_linear_layout);
      
      // Add the first vertical Button regularly.
      Button firstVerticalButton = new Button(Activity.this);
      firstVerticalButton.setText("Vertical");
      verticalLinearLayout.addView(firstVerticalButton);
      
      // Inflate the horizontal LinearLayout and add the two horizontal buttons.
      LinearLayout horizontalLinearLayout = getLayoutInflater().inflate(R.layout.horizontal_layout,
              linearLayout, false);
      
      Button horizontalOne = new Button(Activity.this);
      horizontalOne.setText("Horizontal");
      LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
      childParam1.weight = 0.5f;
      horizontalOne.setLayoutParams(childParam);
      
      Button horizontalTwo = new Button(Activity.this);
      horizontalTwo.setText("Horizontal");
      LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
      childParam1.weight = 0.5f;
      horizontalTwo.setLayoutParams(childParam2);
      
      horizontalLinearLayout.addView(horizontalOne);
      horizontalLinearLayout.addView(horizontalTwo);
      
      // Insert the inflated layout to the main layout.
      verticalLinearLayout.addView(horizontalLinearLayout);
      
      // Add the second vertical Button
      Button secondVerticalButton = new Button(Activity.this);
      secondVerticalButton.setText("Vertical");
      verticalLinearLayout.addView(secondVerticalButton);
      

    【讨论】:

    • 这将使除了这两个按钮之外的所有按钮都是垂直的。
    • 即使您的代码不是以编程方式编写的,您的代码也对我有所帮助,但是我的两个按钮位于 button0 的顶部,如何使其位于中心?我正在以编程方式编写代码
    • 在你膨胀水平LinearLayout之前插入button0,并将它添加到垂直的,所以它将是第一个,然后膨胀水平LinearLayout并插入你想要的两个按钮要水平对齐(button1 + button 2)然后将整个水平Layout添加到垂直Layout,到目前为止button0在顶部,button1 + button2在它下面,最后一步是将button3添加到垂直Layout,你就完成了。
    【解决方案5】:

    使用table.setOrientation(LinearLayout.HORIZONTAL);。

    希望对您有所帮助。

    【讨论】:

    • 我更新了我的问题 :) 带圆圈的按钮是我唯一需要水平对齐的按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2015-11-06
    • 2012-09-16
    相关资源
    最近更新 更多