java - 以编程方式在相对布局中添加按钮网格

标签 java android android-layout

我的 Intent 是以编程方式在相对布局内创建按钮网格。我想以编程方式执行此操作的原因是按钮的数量因情况而异,即我可能需要 12 个按钮而不是 9 个按钮,依此类推。

I managed to do this but with a Linear layout

However, this is the desired outcome

据我所知,我需要在相对布局中创建按钮,而不是 this is what happens当我将布局更改为“相对”时。它们只是相互堆叠。

这是创建按钮的代码:

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);
    }

我花了很多时间试图找出答案并搜索现有答案,但无济于事。任何帮助将不胜感激!

编辑

<item android:state_pressed="true">
    <shape>
        <solid android:color="#449def"/>
        <stroke android:width="1dp" android:color="#2f6699"/>
        <corners android:radius="6dp"/>
        <padding android:left="10dp" android:top="10dp" android:right="10dp"
            android:bottom="10dp"/>
    </shape>
</item>

<item>
    <shape>
        <gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270"/>
        <stroke android:width="1dp" android:color="#2f6699"/>
        <corners android:radius="4dp"/>
        <padding android:left="10dp" android:top="10dp" android:right="10dp"
            android:bottom="10dp"/>
    </shape>
</item>

最佳答案

1.findViewById for RelativeLayout

2.添加外部LinearLayout

3.添加内部LinearLayout和添加Button

4.将Button添加到内部LinearLayout

5.将内部LinearLayout添加到外部LinearLayout

6.将外部 LinearLayout 添加到 RelativeLayout

我在 Activity 类中使用。您可以在使用其他类时更改。 试试这个。

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);
}

编辑

<?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);

输出 enter image description here

关于java - 以编程方式在相对布局中添加按钮网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47192837/

相关文章:

JavaFx 音频文件无法播放

android - MPAndroidChart - 自动调整 Y 值

android - AutoCompleteTextView 在软键盘上的清晰焦点关闭

android - 什么是应用程序标签字符限制?

android - 由于工具栏 Android,部分 RelativeLayout 内容离开屏幕

android-layout - 在合并布局根标签的自定义 View 中应用样式

java - concurrntHashMap 能否同时保证真正的线程安全和并发?

java - 使用读取 rtf 文档中的表格的 JAVA 将 RTF 转换为 PDF

java - 如果java中的字符串是不可变的,为什么我可以这样做?

android - 在列表适配器中膨胀类 com.google.android.youtube.player.YouTubePlayerView 时出错