[英]Android Single Layout to FIt All Screen Sizes
我正在更新我的Android應用程序,並意識到我已經為每個可能的屏幕大小創建了一個布局(布局小,布局大,等等)。瀏覽每個XML文件並手動制作一個小的文件將是一個巨大的痛苦更改。 我正在嘗試創建一個XML文件來支持所有屏幕大小。 在回顧了有關stackoverflow的Android文檔和其他問題之后,似乎LinearLayout是最佳選擇,因為您可以為布局中的每個項目提供weightSum和layout_weight。 這沒有按預期工作(參見下面的代碼和圖像)。 我正確地這樣做了嗎? 我是否必須回去為每個可能的屏幕尺寸創建RelativeLayout?
我的圖像是一個可繪制的文件夾,我的布局在一個布局文件夾中。
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:
片劑:
編輯:我添加了以下drawable -____文件夾。 它產生相同的結果。
您可能需要考慮創建兼容性布局文件夾。 :)
是的,我們通過使用android的百分比布局有相同的解決方案,我們現在可以使用app:layout_heightPercent
和app:layout_widthPercent
來適應所有使用單一布局的屏幕。
compile 'com.android.support:percent:23.0.0'
為什么現在使用LinearLayout weight
屬性我們有更簡單的解決方案。
考慮一個簡單的樣本
<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>
希望它對某人有用:-)
使用下面的布局來排列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>
永遠不要把重量總和像百,只要嘗試使用一位數
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等所有內容......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.