2013-10-31 80 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的,...