大佬教程收集整理的这篇文章主要介绍了android – 按钮应该和最大的一样宽,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我通过在布局中将所有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>
然后我搜索最大值,并将所有按钮设置为相同大小:
@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); } }
<LinearLayout <!--Parent--> android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <!-- Child --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
以上是大佬教程为你收集整理的android – 按钮应该和最大的一样宽全部内容,希望文章能够帮你解决android – 按钮应该和最大的一样宽所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。