开发者

How to make button to fill 50% from the screen width in android?

I want to make the button to fill always 50% from the width of the screen, as when I rotate the device horizontally or vertically always the button fill 50% from the wid开发者_开发百科th.


This should be possible by using weightSum and 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>


To do this in code.

// 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜