如何设置适应任何屏幕高度的布局?

4

我有一个与我的应用程序相关的布局问题。 这是我的应用程序布局:

<?xml version="1.0" encoding="utf-8"?>

<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"/>   
</LinearLayout>

在这里,我希望将所有按钮设置为与任何设备屏幕的高度完全匹配。 是的,我可以显示所有按钮。但是我希望它们之间都有相等的间距,并且它们必须完全适合屏幕高度。 那么我应该怎么做呢?

编辑:

我的tax_calculator_logo分辨率为600x239,我已将ImageView高度设置为wrap_content,宽度为fill_parent,那么为什么图像从底部和顶部裁剪?

2个回答

13

给你的每个按钮分配相同的权重,它们将自动共享高度。

<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>

哇,太棒了。谢谢。你能给我一些关于权重的想法吗?我的意思是我该如何使用它?它会做什么? - Shreyash Mahajan
请看我的编辑问题。 - Shreyash Mahajan
请帮我关于修改后的问题。 - Shreyash Mahajan
在你的imageView中使用android:scaleType="fitXY"。 - Yashwanth Kumar

2
为了做到这一点,我们需要获取显示屏的宽度和高度。然后通过Java代码分配高度。例如,XML文件如下所示:
<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"/>   

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

在调用setContentView(..)方法之后:

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

我认为这可能对你有所帮助...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接