如何使button从android的屏幕宽度填充50%?

我想让button从屏幕的宽度始终填充50%,因为当我旋转设备的水平或垂直总是button填充50%的宽度

Solutions Collecting From Web of "如何使button从android的屏幕宽度填充50%?"

这应该可以通过使用weightSum和layout_weight:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal" android:weightSum="1.0"> <Button android:id="@+id/textbox3" android:layout_height="wrap_content" android:layout_weight=".5" android:layout_width="0dip" android:textSize="12sp" /> </LinearLayout> 

在代码中这样做。

 // Get screen width. DisplayMetrics metrics = new DisplayMetrics(); WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getMetrics(metrics); final float width = metrics.widthPixels; LinearLayout.LayoutParams layoutParamsWidth50 = new LinearLayout.LayoutParams( (int) (width/2) , ViewGroup.LayoutParams.MATCH_PARENT ); textbox3.setLayoutParams(layoutParamsWidth50);