如何以编程方式在eclipse中的同一行上创建两个按钮?

时间:2017-12-09 05:42:02

标签: android eclipse

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

我希望两个按钮水平对齐。然而,它只是垂直对齐,我不想要。

我的按钮看起来像这样:

NO

我想要的是这样: YES

5 个答案:

答案 0 :(得分:2)

尝试以下功能。它会有所帮助

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

}

enter image description here

答案 1 :(得分:1)

  • 您应该使用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

enter image description here

答案 2 :(得分:1)

您可以创建另一个包含水平- name: Snapshoting the instance... ec2_snapshot: region: us-west-2 volume_id: "{{ item }}" description: "Snapshot test by" with_items: "{{ ec2_facts | json_query('instances[].block_device_mapping[].volume_id') }}" 的{​​{1}}文件,然后对其进行充气并将两个按钮插入其中,然后将整个水平XML添加到垂直LinearLayout {1}}:

  1. 创建LinearLayout文件,例如LinearLayout

    XML
  2. 在您的活动中使用此方法来夸大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>
    

答案 3 :(得分:1)

  

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

编写此代码

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

希望这有助于你

答案 4 :(得分:0)

使用table.setOrientation(LinearLayout.HORIZONTAL);

希望它有所帮助。