使用我的应用程序,我按照floatactionbutton动态添加按钮,这样我的按钮就是通过Java生成的。我为我的按钮设置了RelativeLayoutParams'它处于纵向模式时的尺寸,但我需要在Gridlayout @ landscape模式下做同样的事情,但我无法弄清楚如何设置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:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true"
>
<GridLayout
android:id="@+id/semester_grid_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="4"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="myapp.onur.journeygpacalculator.MainActivity">
<android.support.design.widget.FloatingActionButton
android:id="@+id/addActionButton"
android:layout_column="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:onClick="onFloatActionButtonClick"
android:longClickable="true"
app:backgroundTint="@color/colorPrimary"
android:tint="@color/colorWhite"
app:borderWidth="0dp"
android:src="@drawable/ic_add_black_48dp" />
</GridLayout>
</ScrollView>