Полагаю, мое понимание модели макета Android не очень хорошее, но я пытаюсь создать шаблон tableLayout из кнопок 10x10, и они продолжают выходить из экрана / дисплея устройства.
Вот мой макет:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10">
</TableLayout>
<!-- BUTTONS WILL BE HERE-->
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
А мой код просто создает строки и кнопки:
открытый класс MainActivity расширяет AppCompatActivity {LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout tl = findViewById(R.id.table);
for(int i=0;i<10;i++){
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1));
for(int j=0;j<10;j++){
Button b = new Button(this);
b.setText(" ");
//b.setLayoutParams(params); //if I uncomment this, the buttons don't show ???
tr.addView(b);
}
tl.addView(tr);
}
}
}
Спасибо за вашу помощь, и если кто-нибудь может привести меня к хорошему учебнику о том, как создавать пользовательские интерфейсы с Android, я был бы благодарен.