質問編集履歴
1
修正しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,61 +24,126 @@
|
|
24
24
|
マージン関連の属性のせいなのかと思い、すべて変更し確認してみましたが効果はありませんでした。
|
25
25
|
ボタンの大きさを大きくすると空白が生じ始める最小のテキストの大きさも大きくなります。
|
26
26
|
|
27
|
+
|
27
|
-
###ソースコード
|
28
|
+
### ソースコード 追記
|
28
29
|
|
29
30
|
```java
|
30
|
-
//ボタンを配置する部分
|
31
|
-
for (int i = 0; i < (column - 1) * row; i++) {
|
32
|
-
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
|
33
|
-
|
31
|
+
package com.example.myapplication;
|
34
|
-
params.columnSpec = GridLayout.spec(i % (column - 1), 1);
|
35
|
-
params.rowSpec = GridLayout.spec(i / (column - 1), 1);
|
36
32
|
|
33
|
+
import android.content.Context;
|
34
|
+
import android.graphics.Color;
|
37
|
-
|
35
|
+
import android.graphics.drawable.GradientDrawable;
|
36
|
+
import android.os.Bundle;
|
37
|
+
import android.view.MotionEvent;
|
38
38
|
|
39
|
+
import androidx.appcompat.app.AppCompatActivity;
|
40
|
+
import androidx.appcompat.widget.AppCompatButton;
|
39
|
-
|
41
|
+
import androidx.constraintlayout.widget.ConstraintLayout;
|
40
|
-
|
42
|
+
import androidx.gridlayout.widget.GridLayout;
|
41
|
-
Button.setText("");
|
42
|
-
Button.setId(i);
|
43
|
-
Button.setOnClickListener(v -> {
|
44
43
|
|
44
|
+
public class MainActivity extends AppCompatActivity {
|
45
|
-
|
45
|
+
@Override
|
46
|
+
protected void onCreate(Bundle savedInstanceState) {
|
47
|
+
super.onCreate(savedInstanceState);
|
48
|
+
setContentView(R.layout.main);
|
49
|
+
ConstraintLayout constraintLayout = findViewById(R.id.constraint);
|
46
50
|
|
51
|
+
constraintLayout.post(() -> {
|
52
|
+
GridLayout gridLayout = findViewById(R.id.grid);
|
53
|
+
|
54
|
+
int column = gridLayout.getColumnCount();
|
55
|
+
int row = gridLayout.getRowCount();
|
56
|
+
|
57
|
+
int width = constraintLayout.getWidth() / column;
|
58
|
+
int height = constraintLayout.getHeight() / row;
|
59
|
+
int btnSize = 150;
|
60
|
+
int widthMargin = (width-btnSize) / 2;
|
61
|
+
int heightMargin = (height-btnSize) / 2;
|
62
|
+
|
63
|
+
String[] note = {"あ", "あ", "", "あ", "あ"};
|
64
|
+
for (int i = 0; i < column * row; i++) {
|
65
|
+
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
|
66
|
+
params.width = params.height = btnSize;
|
67
|
+
params.columnSpec = GridLayout.spec(i % column, 1);
|
68
|
+
params.rowSpec = GridLayout.spec(i / column, 1);
|
69
|
+
|
70
|
+
params.setMargins(widthMargin, heightMargin, widthMargin, heightMargin);
|
71
|
+
|
72
|
+
CircleButton button = new CircleButton(this,Color.WHITE, btnSize);
|
73
|
+
button.setLayoutParams(params);
|
74
|
+
button.setId(i);
|
75
|
+
button.setText(i % column < note.length ? note[i % column] : null);
|
76
|
+
button.setOnClickListener(v -> {
|
77
|
+
|
78
|
+
});
|
79
|
+
|
47
|
-
|
80
|
+
gridLayout.addView(button);
|
48
|
-
|
81
|
+
}
|
82
|
+
});
|
83
|
+
}
|
84
|
+
|
85
|
+
public static class CircleButton extends AppCompatButton {
|
86
|
+
public CircleButton(Context context,int color, int size) {
|
87
|
+
super(context);
|
88
|
+
GradientDrawable drawable = new GradientDrawable();
|
89
|
+
drawable.setShape(GradientDrawable.OVAL);
|
90
|
+
drawable.setColor(color);
|
91
|
+
setBackground(drawable);
|
92
|
+
|
93
|
+
setPadding(0, 0, 0, 0);
|
94
|
+
setMinWidth(size);
|
95
|
+
setMinHeight(size);
|
96
|
+
setTextSize(100);
|
97
|
+
}
|
98
|
+
|
99
|
+
@Override
|
100
|
+
public boolean onTouchEvent(MotionEvent event) {
|
101
|
+
int centerX = getWidth() / 2;
|
102
|
+
int centerY = getHeight() / 2;
|
103
|
+
int radius = getWidth() / 2;
|
104
|
+
|
105
|
+
if (event.getActionMasked() == MotionEvent.ACTION_DOWN && (float) Math.sqrt(Math.pow(event.getX() - centerX, 2) + Math.pow(event.getY() - centerY, 2)) > radius) {
|
106
|
+
if (event.getAction() == MotionEvent.ACTION_UP) performClick();
|
107
|
+
return false;
|
108
|
+
}
|
109
|
+
|
110
|
+
return super.onTouchEvent(event);
|
111
|
+
}
|
112
|
+
|
113
|
+
@Override
|
114
|
+
public boolean performClick() {
|
115
|
+
return super.performClick();
|
116
|
+
}
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
49
120
|
```
|
50
|
-
```
|
121
|
+
```xml
|
51
|
-
public class CircleButton extends AppCompatButton {
|
52
|
-
public CircleButton(Context context, int color, int size) {
|
53
|
-
super(context);
|
54
|
-
GradientDrawable drawable = new GradientDrawable();
|
55
|
-
drawable.setShape(GradientDrawable.OVAL);
|
56
|
-
|
122
|
+
<?xml version="1.0" encoding="utf-8"?>
|
123
|
+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
124
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
125
|
+
android:id="@+id/constraint"
|
57
|
-
|
126
|
+
android:layout_width="match_parent"
|
127
|
+
android:layout_height="match_parent">
|
58
128
|
|
129
|
+
<androidx.gridlayout.widget.GridLayout
|
130
|
+
android:id="@+id/grid"
|
131
|
+
android:layout_width="0dp"
|
132
|
+
android:layout_height="0dp"
|
133
|
+
android:background="#787878"
|
59
|
-
|
134
|
+
app:columnCount="5"
|
135
|
+
app:layout_constraintBottom_toBottomOf="parent"
|
60
|
-
|
136
|
+
app:layout_constraintEnd_toEndOf="parent"
|
137
|
+
app:layout_constraintStart_toStartOf="parent"
|
138
|
+
app:layout_constraintTop_toTopOf="parent"
|
61
|
-
|
139
|
+
app:rowCount="5">
|
62
|
-
setTextSize(80);
|
63
|
-
}
|
64
140
|
|
65
|
-
|
141
|
+
</androidx.gridlayout.widget.GridLayout>
|
66
|
-
|
142
|
+
</androidx.constraintlayout.widget.ConstraintLayout>
|
67
|
-
int centerX = getWidth() / 2;
|
68
|
-
int centerY = getHeight() / 2;
|
69
|
-
|
143
|
+
```
|
70
144
|
|
71
|
-
if (event.getActionMasked() == MotionEvent.ACTION_DOWN && (float) Math.sqrt(Math.pow(event.getX() - centerX, 2) + Math.pow(event.getY() - centerY, 2)) > radius) {
|
72
|
-
if (event.getAction() == MotionEvent.ACTION_UP) performClick();
|
73
|
-
|
145
|
+
追加で分かったことがあるので記しておきます。
|
74
|
-
|
146
|
+
・ボタンのテキストに何も記述しない列がないと起こらない
|
75
|
-
|
76
|
-
|
147
|
+
・テキストが英文字であると起こらない(かな、カナであると起こる)
|
77
|
-
}
|
78
|
-
|
79
|
-
@Override
|
80
|
-
public boolean performClick() {
|
81
|
-
|
148
|
+
・英文字である列とかなである列が混在していても起こる
|
82
|
-
}
|
83
|
-
}
|
84
|
-
|
149
|
+
どうやらテキストが関係していそうです。
|