How to set this Layout that fit to Any Screen height?

假装没事ソ 提交于 2019-12-03 16:35:31
Yashwanth Kumar

Give equal weight to each of your buttons, they will share height automatically.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical" >

    <Button
        android:id="@+id/contactUs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Contact Us"
        android:textSize="20dp" />

    <Button
        android:id="@+id/contactUs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Contact Us"
        android:textSize="20dp" />

    <Button
        android:id="@+id/contactUs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Contact Us"
        android:textSize="20dp" />

    <Button
        android:id="@+id/contactUs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Contact Us"
        android:textSize="20dp" />

</LinearLayout>

To do this we have to get he display(screen) width and height.Then through java code assign the height. For exmple

The xml file is like this

<ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:src="@drawable/tax_calculator_logo"/>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent"
android:orientation="vertical" android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" android:layout_gravity="center" 
android:layout_marginTop="10dp" android:layout_marginBottom="10dp">

<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:text="Contact Us"
    android:textSize="20dp"/>
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:text="Contact Us"
    android:textSize="20dp"/>   
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:text="Contact Us"
    android:textSize="20dp"/>   
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:text="Contact Us"
    android:textSize="20dp"/>   

In the Activity class

  Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
  int width = display.getWidth();
  int height = display.getHeight()/5;

after setContentView(..);

 imageview.getLayoutParams().height=height;
 button1.getLayoutParams().height=height;
 button2.getLayoutParams().height=height;
 button3.getLayoutParams().height=height;
 button4.getLayoutParams().height=height;

I think this may help you...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!