【问题标题】:Dynamically adding buttons next to each other - RelativeLayout动态添加相邻的按钮 - RelativeLayout
【发布时间】:2017-09-04 12:00:00
【问题描述】:

好的,事情就是这样。我正在尝试为 Android 制作一个类似于钢琴的应用程序,而且我从来没有真正有过 Java 或 Android 编程方面的经验,所以所有这些对我来说都是全新的。我已经设法在 XML 中做到这一点,但我想以编程方式实现它,这样我就可以轻松地添加更多的白键和黑键,这也取决于屏幕尺寸。在 XML 中它看起来像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
    android:id="@+id/white1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white2"
    android:layout_toRightOf="@+id/white1"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white3"
    android:layout_toRightOf="@+id/white2"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white4"
    android:layout_toRightOf="@+id/white3"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white5"
    android:layout_toRightOf="@+id/white4"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white6"
    android:layout_toRightOf="@+id/white5"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#fff"
    android:id="@+id/white7"
    android:layout_toRightOf="@+id/white6"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginStart="-10dp"
    android:layout_marginEnd="-6dp"
    android:background="#000"
    android:id="@+id/black1"
    android:layout_toRightOf="@+id/white1"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginLeft="-6dp"
    android:layout_marginRight="-10dp"
    android:background="#000"
    android:id="@+id/black2"
    android:layout_toRightOf="@+id/white2"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginLeft="-10dp"
    android:layout_marginRight="-6dp"
    android:background="#000"
    android:id="@+id/black3"
    android:layout_toRightOf="@+id/white4"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginLeft="-8dp"
    android:layout_marginRight="-8dp"
    android:background="#000"
    android:id="@+id/black4"
    android:layout_toRightOf="@+id/white5"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginLeft="-6dp"
    android:layout_marginRight="-10dp"
    android:background="#000"
    android:id="@+id/black5"
    android:layout_toRightOf="@+id/white6"/>

现在我想以编程方式重新创建它,起初我尝试过线性方法,但首先我无法制作超过 7 个键,而且我真的不知道如何在此之上制作黑键.所以现在我使用了RelativeLayout,只要我创建两个按钮,一切都很好,然后它工作正常,一个紧挨着另一个。但是当我尝试创建两个以上的按钮时,它们有点堆叠。

我试图制作某种按钮数组,这样我就可以轻松地创建一个循环来创建指定数量的按钮。我也想改变按钮的宽度,所以如果我创建 8 个按钮,那么它的宽度将为 screen_width/8 但我不太确定这是否有意义,因为它在未注释时实际上没有做任何事情。

如果有任何提示,我将不胜感激:)

public class MainActivity extends AppCompatActivity {

final int[] whitelist = {R.id.whitebt1,R.id.whitebt2,R.id.whitebt3,R.id.whitebt4,R.id.whitebt5,
        R.id.whitebt6,R.id.whitebt7,R.id.whitebt8};
Button[] whiteKeys = new Button[whitelist.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;



    final RelativeLayout pianoLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams whiteKeyParams1 =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


    whiteKeys[0] = new Button(this);
    whiteKeys[0].setId(View.generateViewId());
    //whiteKeys[0].setHeight(height);
    //whiteKeys[0].setWidth(width/8);
    whiteKeys[0].setLayoutParams(whiteKeyParams1);
    pianoLayout.addView(whiteKeys[0]);


    whiteKeys[1] = new Button(this);
    whiteKeys[1].setId(View.generateViewId());
    //whiteKeys[i].setHeight(height);

    RelativeLayout.LayoutParams whiteKeyParams2 =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    whiteKeyParams2.addRule(RelativeLayout.RIGHT_OF, whiteKeys[0].getId() );
    whiteKeys[1].setLayoutParams(whiteKeyParams2);
    pianoLayout.addView(whiteKeys[1]);

    //HERE'S IS THE MOMENT WHERE I TRY TO ADD THIRD BUTTON AND THE BUTTONS START TO PILE UP
    /*
    whiteKeys[2] = new Button(this);
    whiteKeys[2].setId(View.generateViewId());
    //whiteKeys[i].setHeight(height);

    //RelativeLayout.LayoutParams whiteKeyParams2 =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    whiteKeyParams2.addRule(RelativeLayout.END_OF, whiteKeys[1].getId());
    whiteKeys[2].setLayoutParams(whiteKeyParams2);
    pianoLayout.addView(whiteKeys[2]);*/

    this.setContentView(pianoLayout);
    }
}

【问题讨论】:

    标签: java android button dynamic programmatically-created


    【解决方案1】:

    您可以使用weightsumlayoutweight 添加8 个相同大小的按钮,LienarLayout 具有水平方向。

    请参阅下面的代码,它可以帮助您动态添加相同大小的按钮。

      /* Add a new Linearlayout as a container for the buttons */
                LinearLayout linearLayout = new LinearLayout(this);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                //Added Weight Sum 8 in LinearLayout
                linearLayout.setWeightSum(8);
                /* Create a new Buttons in this container, for the status bar */
                //below LayoutParams define with weight 1 for buttons.
                LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
                Button button1 = new Button(linearLayout.getContext());
                button1.setLayoutParams(param);
    
                Button button2 = new Button(linearLayout.getContext());
                button2.setLayoutParams(param);
    
                Button button3 = new Button(linearLayout.getContext());
                button3.setLayoutParams(param);
    
                Button button4 = new Button(linearLayout.getContext());
                button4.setLayoutParams(param);
    
                Button button5 = new Button(linearLayout.getContext());
                button5.setLayoutParams(param);
    
                Button button6 = new Button(linearLayout.getContext());
                button6.setLayoutParams(param);
    
                Button button7 = new Button(linearLayout.getContext());
                button7.setLayoutParams(param);
    
                Button button8 = new Button(linearLayout.getContext());
                button8.setLayoutParams(param);
    

    【讨论】:

    • 太好了:)。但是我确实有一个问题,如何在此之上添加另一个键?我的意思是,就像在钢琴中一样,是否可以像在 XML 中那样使用 LinearLayout 来做到这一点?或者,也许我必须创建一个相对布局,并在其中创建两个线性布局,一个是底部的白键,另一个是顶部的黑键?
    【解决方案2】:

    在将视图添加到父布局之前,您必须为每个新键添加边距,这也将防止将一个键堆叠在另一个键上。

    params.setMargins(left, top, right, bottom);

    【讨论】:

    • 它真的不适合我。我的意思是只要只有两个按钮它就可以工作。但是当我尝试添加第三个时,所有这些都堆叠起来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多