以编程方式在相对布局

时间:2017-11-09 02:17:35

标签: java android android-layout

我的目的是以编程方式在相对布局中创建按钮网格。我想以编程方式进行操作的原因是因为按钮的数量因情况而异,即我可能需要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>

2 个答案:

答案 0 :(得分:0)

1. findViewById RelativeLayout

2.add outer LinearLayout

3.add inner 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

答案 1 :(得分:0)

我认为您只需要在按钮布局文件中添加layout_margin(但使用线性布局而不是相对布局),例如:

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