有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将GridLayout参数设置为动态添加视图的GridLayout?

在我的应用程序中,我每次点击floatactionbutton都会动态添加按钮,所以我的按钮是通过Java生成的。在纵向模式下,我已经为按钮的尺寸设置了RelativeLayoutParams,但在Gridlayout@横向模式下,我需要做同样的设置,但我不知道如何设置Gridlayout。我的floatactionbutton也有一个问题,当视图超过屏幕大小时单击它,按钮也会向下滚动

非常感谢您的帮助

这是我的Java

public class MainActivity extends AppCompatActivity {

    int counter = 0;

    FloatingActionButton addingSemester;
    Button semesterButton;
    LinearLayout semesterLayout;
    GridLayout semesterGridLayout;

    RelativeLayout.LayoutParams portraitLayoutParams = new RelativeLayout.LayoutParams(
            AppBarLayout.LayoutParams.MATCH_PARENT,
            AppBarLayout.LayoutParams.WRAP_CONTENT);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addingSemester = (FloatingActionButton) findViewById(R.id.addActionButton);
        semesterLayout = (LinearLayout) findViewById(R.id.main_layout);
        semesterGridLayout = (GridLayout)findViewById(R.id.semester_grid_layout);
        semesterButton = new Button(MainActivity.this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

            if (id == R.id.delete) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Delete entry")
                        .setMessage("Are you sure you want to delete everything?")
                        .setCancelable(true)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
                                    semesterGridLayout.removeAllViews();
                                } else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
                                    semesterLayout.removeAllViews();
                                }
                                counter = 0;
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        })
                        .show();
                return true;
            }


        return super.onOptionsItemSelected(item);

    }

    public void onFloatActionButtonClick(View view) {
        semesterButton = new Button(MainActivity.this);
        if (counter < 8) {
            semesterButton.setId(counter + 1);
            semesterButton.setText("Semester " + (counter + 1));
            semesterButton.setBackgroundColor(getColor(R.color.colorPrimary));
            semesterButton.setTextColor(Color.WHITE);
            portraitLayoutParams.setMargins(24, 24, 24, 24);

            if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
                semesterGridLayout.addView(semesterButton);
            } else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
                semesterLayout.addView(semesterButton);
                semesterButton.setLayoutParams(portraitLayoutParams);
            }

            // these lines moved outside the if statement blocks
            counter++;
            setOnLongClickListenerForSemesterButton();

        } else if (counter == 8) {
            Toast.makeText(MainActivity.this, "You cannot add more than 8 semesters", Toast.LENGTH_SHORT).show();
        }
    }

    private void setOnLongClickListenerForSemesterButton() {
        semesterButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final Button b = (Button) v;
                b.setTag(b.getText().toString());
                b.setBackgroundColor(Color.RED);
                b.setText("Delete");

                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Delete entry")
                        .setMessage("Are you sure you want to delete this entry?")
                        .setCancelable(true)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                if (MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
                                    semesterGridLayout.removeView(b);
                                } else if (!MainActivity.this.getResources().getBoolean(R.bool.is_landscape)) {
                                    semesterLayout.removeView(b);
                                }
                                counter--;
                                for (int i = 0; i < semesterGridLayout.getChildCount(); i++) {
                                    ((Button) semesterGridLayout.getChildAt(i)).setText("Semester " + (i + 1));
                                }
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                b.cancelLongPress();
                                b.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
                                b.setText(b.getTag().toString());
                                dialog.cancel();

                            }
                        })
                        .show();
                return true;
            }
        });
    }
}

这是我的用于横向模式的XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_height="match_parent"
    安卓:layout_width="match_parent"
    安卓:fillViewport="true"
    >

    <GridLayout
        安卓:id="@+id/semester_grid_layout"
        xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
        xmlns:tools="http://schemas.安卓.com/tools"
        安卓:background="#FFFFFF"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:columnCount="3"
        安卓:rowCount="4"
        xmlns:app="http://schemas.安卓.com/apk/res-auto"
        tools:context="myapp.onur.journeygpacalculator.MainActivity">


            <安卓.support.design.widget.FloatingActionButton
                安卓:id="@+id/addActionButton"
                安卓:layout_column="2"
                安卓:layout_width="wrap_content"
                安卓:layout_height="wrap_content"
                安卓:layout_margin="16dp"
                安卓:clickable="true"
                安卓:onClick="onFloatActionButtonClick"
                安卓:longClickable="true"
                app:backgroundTint="@color/colorPrimary"
                安卓:tint="@color/colorWhite"
                app:borderWidth="0dp"
                安卓:src="@drawable/ic_add_black_48dp" />

    </GridLayout>
    </ScrollView>

共 (0) 个答案