2011-10-03 90 views
3

我有一些關於我的應用程序的佈局問題。 這是我的應用程序佈局:如何設置適合任何屏幕高度的佈局?

<?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那麼爲什麼圖像從底部和頂部croping ???

回答

10

爲每個按鈕賦予相同的權重,他們將自動共享高度。

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

哇。它的工作很棒。謝謝。你能給我一些關於體重的想法嗎?我的意思是我如何使用它?它會做什麼? –

+0

精湛.....解決方案。 –

+0

請參閱我編輯的問題。 。 。 –

2

要做到這一點,我們必須讓他顯示(屏幕)寬度和高度。然後通過java代碼分配高度。對於exmple

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

在活動類

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; 

我覺得這可能會幫助你...

相關問題