Java 动态添加彼此相邻的按钮-RelativeLayout
好吧,事情是这样的。我正在尝试制作一款类似于安卓钢琴的应用程序,而且我从来没有真正的Java经验或安卓编程经验,所以所有这些对我来说都是全新的。我已经成功地用XML实现了这一点,但我想让它成为可编程的,这样我就可以根据屏幕大小轻松地添加更多的黑白键。在XML中,它是这样的Java 动态添加彼此相邻的按钮-RelativeLayout,java,android,button,dynamic,programmatically-created,Java,Android,Button,Dynamic,Programmatically Created,好吧,事情是这样的。我正在尝试制作一款类似于安卓钢琴的应用程序,而且我从来没有真正的Java经验或安卓编程经验,所以所有这些对我来说都是全新的。我已经成功地用XML实现了这一点,但我想让它成为可编程的,这样我就可以根据屏幕大小轻松地添加更多的黑白键。在XML中,它是这样的 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
<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"/>
您可以使用
weightsum
和layoutweight
以及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);
您可以使用
weightsum
和layoutweight
以及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);
在将视图添加到父布局之前,您必须为每个新关键点添加边距,这将防止一个关键点堆叠在另一个关键点上
参数设置边距(左、上、右、下) 在将视图添加到父布局之前,您必须为每个新关键点添加边距,这将防止一个关键点堆叠在另一个关键点上
参数设置边距(左、上、右、下) 这对我来说真的不管用。我的意思是只要只有两个按钮就行。但是,当我尝试添加第三个时,它们都会堆积起来。这对我来说并不起作用。我的意思是只要只有两个按钮就行。但是当我尝试添加第三个时,它们都会堆叠起来。这很好:)。但我有一个问题,我如何在上面再添加一个键?我的意思是,就像在钢琴上一样,是否可以像在XML中那样使用LinearLayout?或者我必须创建一个相对的布局,在它里面有两个线性布局,一个是底部的白键,另一个是顶部的黑键?太好了:)。但我有一个问题,我如何在上面再添加一个键?我的意思是,就像在钢琴上一样,是否可以像在XML中那样使用LinearLayout?或者我必须创建一个相对的布局,在里面有两个线性布局,一个是底部的白键,另一个是顶部的黑键?