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

时间:2011-10-03 09:49:19

标签: android android-layout android-emulator android-widget

我的应用程序有一些布局问题。 这是我的应用程序布局:

<?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,然后为什么Image从底部和顶部裁剪???

2 个答案:

答案 0 :(得分:11)

为每个按钮赋予相同的重量,它们将自动共享高度。

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

答案 1 :(得分: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;

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