Java 安卓-如何根据屏幕大小以编程方式添加按钮,并动态地在每个按钮下方添加按钮?
我在制作Android应用程序时遇到了一个小问题。 我希望有一个反应灵敏和动态的应用程序 当用户单击按钮时,我想动态添加按钮,下面是我的代码:Java 安卓-如何根据屏幕大小以编程方式添加按钮,并动态地在每个按钮下方添加按钮?,java,android,button,layout,Java,Android,Button,Layout,我在制作Android应用程序时遇到了一个小问题。 我希望有一个反应灵敏和动态的应用程序 当用户单击按钮时,我想动态添加按钮,下面是我的代码: @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_ajouter: Button b = new Button(this);
@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;
}
问题是:我需要将两个按钮并排添加,另一个按钮(也并排)吹出这些按钮。问题是它们重叠在一起,没有添加到其他按钮的下面。由于布局参数不合适,您的按钮彼此重叠:
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++;
就这些
编辑:这里有一些代码,不知道这是不是你的需要
在这个应用程序中,我在滚动视图和按钮中有一个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);
}
}
活动_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>
结果如下:
检查此代码:
// 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);
你的问题是什么?我需要两个按钮并排添加,另一个按钮(也并排)吹这些按钮。问题是它们叠加在一起,没有添加到其他按钮的下方。我看不到屏幕上的按钮,我还使用ScrollView添加了许多需要的按钮。我是否需要更改其他内容?您的视图结构如何?你在ScrollView中放了一个带有param的线性布局吗?我的布局文件中只有一个ScrollView,然后我在Java代码中添加了LinearLayout,正如你所提到的。My LinearLayout使用以下参数获取这些参数:LinearLayout.LayoutParams params=新建LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.WRAP_内容);当我使用你的方法时,当我添加按钮时,它们会从屏幕右侧消失。我用演示代码更新了我的答案,你可以看一看。它是有效的,但这不是我等待的结果@田北辰展示了我想要的。