Java 以编程方式在相对布局中添加按钮网格
我的目的是以编程方式在相对布局中创建按钮网格。我之所以希望以编程的方式来做这件事,是因为按钮的数量因情况而异,也就是说,我可能需要12个按钮,而不是9个,以此类推 据我所知,我需要在相对布局中创建按钮,但是当我将布局更改为相对布局时。。它们只是叠在一起 以下是创建按钮的代码:Java 以编程方式在相对布局中添加按钮网格,java,android,android-layout,Java,Android,Android Layout,我的目的是以编程方式在相对布局中创建按钮网格。我之所以希望以编程的方式来做这件事,是因为按钮的数量因情况而异,也就是说,我可能需要12个按钮,而不是9个,以此类推 据我所知,我需要在相对布局中创建按钮,但是当我将布局更改为相对布局时。。它们只是叠在一起 以下是创建按钮的代码: for (int i = 0; i < frows; i++) { LinearLayout row = new LinearLayout(this); row.setLayout
for (int i = 0; i < frows; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER_HORIZONTAL);
row.setPadding(0, 40, 0, 0);
for (int j = 0; j < 3; j++) {
ContextThemeWrapper newContext = new ContextThemeWrapper(getBaseContext(), R.style.ExerciseButtonTheme);
eBtn = new Button(newContext);
eBtn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
eBtn.setText("" + (j + 1 + (i * 3)));
eBtn.setId(j + 1 + (i * 3));
eBtn.setBackgroundResource(R.drawable.exercisebutton);
row.addView(eBtn);
eBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), ListActivity.class);
id = "" + view.getId();
intent.putExtra(EXTRA_MESSAGE, id);
startActivity(intent);
}
});
}
layout.addView(row);
}
for(int i=0;i
我花了很多时间试图找出它,并寻找现有的答案,但没有结果。任何帮助都将不胜感激
编辑
1.
findViewById
forRelativeLayout
2.添加外部LinearLayout
3.添加内部线性布局
和添加按钮
4.将按钮
添加到内部线性布局
5.将内部LinearLayout
添加到外部LinearLayout
6.将外部线性布局
添加到相对布局
我在活动
类中使用。当您使用其他类时,您可以更改。
试试这个
private RelativeLayout mRelativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your);
// findViewById for RelativeLayout
mRelativeLayout = (RelativeLayout) findViewById(R.id.your_relative);
// add LinearLayout
LinearLayout linear = new LinearLayout(this);
linear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// set setOrientation
linear.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER_HORIZONTAL);
row.setPadding(0, 40, 0, 0);
for (int j = 0; j < 3; j++) {
Button eBtn = new Button(this);
eBtn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
eBtn.setText("" + (j + 1 + (i * 3)));
eBtn.setId(j + 1 + (i * 3));
eBtn.setBackgroundResource(R.drawable.exercisebutton);
// add view to the inner LinearLayout
row.addView(eBtn);
eBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), ListActivity.class);
id = "" + view.getId();
intent.putExtra(EXTRA_MESSAGE, id);
startActivity(intent);
}
});
}
// add view to the outer LinearLayout
linear.addView(row);
}
mRelativeLayout.addView(linear);
}
输出
我认为您只需要在按钮布局文件中添加布局边距(但使用线性布局而不是相对布局),例如:
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/eBtn"/>
我试过了,但还是无法分开按钮?尝试调整按钮的样式,但安卓:layout_marginLeft什么也没做。我认为这是因为按钮仍然在线性布局中,所以它们总是紧挨着彼此?它看起来和我在原始帖子中链接的第一张图片一模一样。行之间的分隔很好,但我无法将每个按钮彼此分隔。您应该删除代码。然后重试。您可以看到输出
。您也可以显示xml文件吗?我一定是对样式做了一些错误,因为我复制粘贴了您的代码,但仍然无法正常工作。您在哪里为按钮本身设置样式?您只是喜欢eBtn.setBackgroundResource(R.drawable.exercisebutton)代码>setTextColor
。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
</LinearLayout>
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,10,10,10);
yourBtn.setLayoutParams(layoutParams);
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/eBtn"/>