【问题标题】:Android - How to add buttons programmatically depending on screen size and button below each other dynamically?Android - 如何根据屏幕大小和按钮动态添加按钮?
【发布时间】:2016-08-01 07:09:10
【问题描述】:

我在制作 Android 应用时遇到了一点问题。 我想要一个响应式和动态的应用程序。

当用户点击按钮时,我想动态添加按钮,这是我的代码:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {


    switch (item.getItemId()) {
        case R.id.action_ajouter:

            Button b = new Button(this);


               //numButton counts the number of buttons created by
              //clicking on a button placed in the action bar

                if (numButton % 2 != 0) {
                    //Align to the left

                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    b.setLayoutParams(params);
                    b.setText("Bouton n°" + numButton);
                    mainRelativeLayout.addView(b);
                    numButton++;

                } else {
                    //Align to the right
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    b.setLayoutParams(params);
                    b.setText("Bouton n°" + numButton);
                    mainRelativeLayout.addView(b);
                    numButton++;
                }

            break;
        default:
            break;
    }
    return true;


    }

问题是: 我需要并排添加两个按钮,而其他按钮会破坏这些按钮(也并排)。问题是它们将它们叠加在一起,而不是被添加到其他下面。

【问题讨论】:

  • 你的问题是什么?
  • 我需要并排添加两个按钮,而其他按钮会破坏这些按钮(也并排)。问题是它们将它们叠加在一起,而不是被添加到其他下面。

标签: java android button layout


【解决方案1】:

您的按钮相互重叠,因为您的布局参数不合适:

params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                b.setLayoutParams(params);
                b.setText("Bouton n°" + numBoutton);

使用上面的代码,所有按钮都将与父级左对齐。

如果要在其他按钮旁边添加按钮,最好使用 LinearLayout 而不是 RelativeLayout。

假设您已经有一个水平方向的线性布局。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                b.setLayoutParams(params);
                b.setText("Bouton n°" + numBoutton);
                mainLinearLayout.addView(b);
                numBoutton++;

就是这样。

编辑:这里有一些代码,不知道你是否需要。

在这个应用程序中,我在 ScrollView 和 Button 中有一个 LinearLayout。当我单击按钮时,我将新按钮添加到 ScrollView。

MainActivity.java

package com.tiennt.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private LinearLayout llButtons;
    private int buttonCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnAdd = (Button) findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doAddButton();
            }
        });
        llButtons = (LinearLayout) findViewById(R.id.ll_buttons);
    }

    private void doAddButton() {
        Button button = new Button(this);
        button.setText("Button " + ++buttonCount);
        button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        llButtons.addView(button);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.tiennt.myapplication.MainActivity">

    <Button
        android:id="@+id/btn_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD BUTTON"
        android:layout_gravity="center_horizontal" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/ll_buttons"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

    </ScrollView>

</LinearLayout>

结果如下:

【讨论】:

  • 我看不到屏幕上的按钮,我还使用 ScrollView 根据需要添加许多按钮。我需要更改其他内容吗?
  • 您的视图结构如何?您是否在 ScrollView 中放置了带有 参数的线性布局?
  • 我的布局文件中只有一个 ScrollView,然后我添加了 Java 代码,正如你提到的那样,LinearLayout。我的 LinearLayout 得到了这些参数: LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  • 当我使用你的方法时,当我添加按钮时,它们会从屏幕右侧消失。
  • 我用演示代码更新了我的答案,你可以看看。
【解决方案2】:

检查此代码:

   // first Button
            RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);
            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            Button b1 = new Button(this);
            b1.setText("Hello button 1");
            b1.setLayoutParams(param);
            b1.setId(1);
            relativeLayout.addView(b1);

            // second Button RIGHT_OF
            RelativeLayout.LayoutParams paramRightOf = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            Button b2 = new Button(this);
            b1.setText("Hello button 2");
            paramRightOf.addRule(RelativeLayout.RIGHT_OF, b1.getId());
            b2.setLayoutParams(paramRightOf);
            b2.setId(2);
            relativeLayout.addView(b1);

            // add third button BELOW
            RelativeLayout.LayoutParams paramBelow = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            Button b3 = new Button(this);
            b1.setText("Hello button 3");
            paramBelow.addRule(RelativeLayout.BELOW, b1.getId());
            b3.setLayoutParams(paramBelow);
            b3.setId(3);
            relativeLayout.addView(b3);

【讨论】:

  • 它有效,但这不是我等待的结果。 @Tien 展示了我想要的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 2017-12-02
  • 2018-01-20
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多