I want all Button match the text, so there are no line breaks in Button s, but all Button should be the same width: as wide as the largest.
I achieved this with setting in Button layout_width WRAP_CONTENT :
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> ... </LinearLayout>
Then I search for the biggest value and set all buttons of the same size:
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); ViewGroup layout = (ViewGroup) findViewById(R.id.layout); int size = 0; for (int i = 0; i < layout.getChildCount(); ++i) { View child = layout.getChildAt(i); if (child.getWidth() > size) { size = child.getWidth(); } } for (int i = 0; i < layout.getChildCount(); ++i) { LayoutParams params = layout.getChildAt(i).getLayoutParams(); params.width = size; layout.getChildAt(i).setLayoutParams(params); } }
Are there any cleaner, more elegant solutions for this?
android android-layout view android-view
Wondercsabo
source share