【问题标题】:How to set this Layout that fit to Any Screen height?如何设置适合任何屏幕高度的布局?
【发布时间】:2011-11-29 19:24:16
【问题描述】:

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

<?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 那么为什么图像从底部和顶部裁剪???

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 哇。 .. 它的工作很棒。谢谢。你能给我一些关于重量的想法吗?我的意思是我如何使用它?它会做什么???
    • 请参阅我的编辑问题。 . .
    • 请帮助我处理已编辑的问题。
    • 使用 android:scaleType="fitXY",为你的 imageView 可能。
    【解决方案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;
    

    我想这可能会对你有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      相关资源
      最近更新 更多