Я строю пирамиду кнопок и хочу, чтобы размер пирамиды мог динамически изменяться. Для этого у меня есть очень простые XML-файлы, представляющие действие, каждую строку действия и каждую кнопку. Я моделирую решение из принятого ответа на этот вопрос . Пирамида строится правильно, но ширина кнопки 50dip не соблюдается. Есть идеи почему? Есть ли лучший способ сделать это?
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pyramid"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
/>
row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"/>
btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
Основная деятельность:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflate();
}
private void inflate() {
LinearLayout pyramidLayout = (LinearLayout) findViewById(R.id.pyramid);
for (int row = 1 ; row <= mSize; ++row) {
View rowView = getLayoutInflater().inflate(R.layout.row, null);
LinearLayout rowLayout = (LinearLayout) rowView.findViewById(R.id.row);
for (int column = 1; column <= row; ++column) {
View btnView = getLayoutInflater().inflate(R.layout.btn, null);
Button btn = (Button) btnView.findViewById(R.id.button);
btn.setId(row*10 + column);
rowLayout.addView(btnView);
}
pyramidLayout.addView(rowView);
}
}