【问题标题】:Programmatically adding a grid of buttons inside Relative layout以编程方式在相对布局中添加按钮网格
【发布时间】:2018-05-02 11:02:19
【问题描述】:

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

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    1.findViewById 代表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);
    

    输出

    【讨论】:

    • 我试过了,但我仍然无法分离按钮?尝试调整按钮的样式,但 android:layout_marginLeft 什么也没做。我认为这是因为按钮仍在线性布局内,所以它们总是彼此相邻?它看起来与我在原始帖子中链接的第一张图片完全相同。这些行分开得很好,但我无法将每个单独的按钮彼此分开..
    • 你应该删除你的代码。然后再试一次。你可以看到output
    • 你能把xml文件也给我看看吗?我一定是样式做错了,因为我复制粘贴了您的代码,但它仍然无法正常工作
    • 你在哪里设置按钮的样式?
    • 你喜欢eBtn.setBackgroundResource(R.drawable.exercisebutton); .setTextColor .
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-09-18
      • 1970-01-01
      • 2013-07-05
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多