gpt4 book ai didi

java - 动态添加彼此相邻的按钮-RelativeLayout

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:46 25 4
gpt4 key购买 nike

好的,事情就是这样。我正在尝试制作一个类似于 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);
}
}

最佳答案

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

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

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

关于java - 动态添加彼此相邻的按钮-RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43306273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com