2013-10-31 81 views
1

我更新我的Android應用程序,並意識到,我已經創建了一個佈局爲每個可能的屏幕尺寸(佈局小,佈局大型,等...),這將是一個巨大的痛苦瀏覽每個XML文件並手動進行小改動。我正在嘗試創建一個XML文件來支持所有的屏幕尺寸。在查看了Android文檔和其他關於stackoverflow的問題之後,似乎LinearLayout是最好的選擇,因爲您可以爲佈局中的每個項目提供weightSum和layout_weight。這不像預期的那樣工作(見下面的代碼和圖像)。我正確地做到了這一點?我是否必須回到爲每種可能的屏幕尺寸創建RelativeLayout?的Android單個佈局,以適應所有屏幕尺寸

我的圖像是一個繪製的文件夾和我的佈局是在一個單一的佈局文件夾。

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:weightSum="100" > 

    <ImageButton 
     android:id="@+id/btn1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 
     android:src="@drawable/image0" 
     android:background="@null" 
     android:layout_weight="30" /> 

    <ImageButton 
     android:id="@+id/btn2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 
     android:src="@drawable/image1" 
     android:background="@null" 
     android:layout_weight="30" /> 

    <ImageButton 
     android:id="@+id/key" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="30" 
     android:background="@null" 
     android:src="@drawable/image0_key" /> 

    <TextView 
     android:id="@+id/tvScore" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="Score: 0" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_weight="10" 
     android:layout_gravity="left" /> 

</LinearLayout> 

結果視圖

歌Nexus One(項目及佈局的屏幕尺寸不相符的溢出):

enter image description here

平板電腦:

enter image description here

編輯: 我已經添加了以下可繪製-____文件夾。它產生相同的結果。

enter image description here

+0

每個孩子的layout_height應0dp和根據重量分配。你的textview也有一個match_parent,這將強制電視佔用和父母一樣多的空間(覆蓋所有屏幕的線性佈局) –

+0

將每個孩子的layout_height更改爲0dp後,沒有任何變化。看起來和上面的照片完全一樣。 – Shane

+0

你需要了解重量是如何工作的。佈局重量在佈局測量完成後(第二遍)分佈,因此您必須提供每個顯示密度具有相同密度的圖像,否則不需要做任何事情。系統在放置圖像後使用FREE空間。 –

回答

2

你可能要考慮創建兼容性佈局文件夾。 :)

enter image description here

+1

這就是我想要剪掉的東西。我只想爲每個設備創建一個佈局,因此在進行小的更改後我不必編輯所有設備。 – Shane

0

使用下面的佈局安排您的ImageButton和TextView的。它適用於所有屏幕尺寸佈局。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="3" /> 

<ImageButton 
    android:id="@+id/imageBtn1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/ic_launcher" /> 

<ImageButton 
    android:id="@+id/imageBtn2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/tv1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="Score: 0" /> 

</LinearLayout> 
0

不要把一個重量和像百,只是嘗試使用個位數

2

是的,我們必須使用Android的百分之佈局同一個解決方案,我們現在可以使用app:layout_heightPercentapp:layout_widthPercent以適合所有使用單一佈局的屏幕。

compile 'com.android.support:percent:23.0.0' 

爲什麼要使用的LinearLayout weight屬性現在我們有簡單的解決方案。

Demo HERE !

GitHub project HERE !

考慮一個簡單的示例

<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/fifty_huntv" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="#ff7acfff" 
     android:text="20% - 50%" 
     android:textColor="@android:color/white" 
     app:layout_heightPercent="20%" 
     app:layout_widthPercent="50%" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_toRightOf="@id/fifty_huntv" 
     android:background="#ffff5566" 
     android:text="80%-50%" 
     app:layout_heightPercent="80%" 
     app:layout_widthPercent="50%" 
     /> 

</android.support.percent.PercentRelativeLayout> 

enter image description here

希望這是有用的人:-)

因爲高度基於可用空間計算
0
DisplayMetric dm =new DisplayMetric(); 

getWindowManager().getDefaultDisplay().getMetrics(dm); 
int h= dm.heightPixels; 
int w= dm.widthPixels; 
h=h/10; // 10 is for example for 10% of display height 
w=((w*20)/100); // 20%of display width 
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(w,h); 
YourObject.setLayoutParams(params); 

//(YourObject)可凡事比如按鈕,圖像按鈕,TextView的,...