简体   繁体   English

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

[英]Programmatically adding a grid of buttons inside Relative layout

My intention is to programmatically create a grid of buttons inside a Relative Layout. 我的目的是在相对布局中以编程方式创建按钮网格。 The reason why I want to do it programmatically is because the number of buttons varies by situation, ie I might need 12 buttons instead of 9 and so on. 我之所以要以编程方式进行操作,是因为按钮的数量因情况而异,即我可能需要12个按钮,而不是9个,依此类推。

I managed to do this but with a Linear layout 我设法做到了,但是使用了线性布局

However, this is the desired outcome 但是,这是理想的结果

As far as I can tell, I need to create the buttons inside a Relative Layout instead but this is what happens when I change the layout to Relative.. They just stack on top of each other. 据我所知,我需要在“相对布局”中创建按钮,但是当我将布局更改为“相对”时会发生这种情况 。它们只是堆叠在一起。

Here is the code that creates the buttons: 这是创建按钮的代码:

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

I've spent a lot of time trying to figure it out and search for existing answers but to no avail. 我花了很多时间试图找出答案并寻找现有答案,但无济于事。 Any help will be greatly appreciated! 任何帮助将不胜感激!

EDIT 编辑

<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 1.为RelativeLayout findViewById

2.add outer LinearLayout 2.添加外部LinearLayout

3.add inner LinearLayout and add Button 3.添加内部LinearLayout并添加Button

4.add Button to the inner LinearLayout 4.添加Button到内部LinearLayout

5.add inner LinearLayout to the outer LinearLayout 5,将内部LinearLayout到外部LinearLayout

6.add outer LinearLayout to the RelativeLayout . 6.将外部LinearLayoutRelativeLayout

And I use in the Activity class .And You can change when you use other class . 我在Activity类中使用。并且在使用其他类时可以更改。 Try this . 尝试这个 。

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

Edit 编辑

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

Edit 编辑

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,10,10,10);
yourBtn.setLayoutParams(layoutParams);

OUTPUT 输出值 在此处输入图片说明

I think u just need to add layout_margin in your button layout file (but using Linear Layout instead of Relative Layout), example: 我认为您只需要在按钮布局文件中添加layout_margin(但使用线性布局而不是相对布局),例如:

<Button
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:id="@+id/eBtn"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM