2011-12-11 72 views
0

我有4项在我的应用程序的开始屏幕,看起来像下面这样: enter image description here规模布局项目

什么是重要的是我有: - 所有项目都具有相同的宽度(不涉及多少文本实际上) - 看起来相同的所有设备(小屏幕,mdpi,大屏幕等)

我只是想知道是否有关于这个问题的简单解决方案?

我试过使用3 LinearLayouts,但那真的很尴尬.. (1呈现布局根[vertical]和两个每个包含2个按钮[horizo​​nal])。 使这个布局准备好多个屏幕将需要大量的固定宽度和固定边缘黑客行为。就像“xlarge上的按钮边距= 30dp,20个大,15个正常......”。

我的布局XML:

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

<LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_gravity="center" > 

    <Button 
       android:id="@+id/btn_learn" 
       android:text="@string/mainBtn_learn" 
       style="@style/mainBtn" 
       android:onClick="handleBtnClick" 
       android:layout_margin="20dip" /> 

    <Button 
       android:id="@+id/btn_quiz" 
       android:text="@string/mainBtn_quiz" 
       style="@style/mainBtn" 
       android:onClick="handleBtnClick" 
       android:layout_margin="20dip" /> 

</LinearLayout> 

<LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_gravity="center" > 

    <Button 
       android:id="@+id/btn_search" 
       android:text="@string/mainBtn_search" 
       style="@style/mainBtn" 
       android:onClick="handleBtnClick" 
       android:layout_margin="20dip" /> 

    <Button 
       android:id="@+id/btn_more" 
       android:text="@string/mainBtn_more" 
       style="@style/mainBtn" 
       android:onClick="handleBtnClick" 
       android:layout_margin="20dip" /> 

</LinearLayout> 

是否有一个视图,其“自动秤”这些按钮或静止任何其他简单的解决方案?

编辑:

所以,在特殊的,你需要像 按钮:

android:layout_width="15%" // 15% of screen width/height depending on the orientation 
android:layout_marginBottom="10%" // see above 

回答

1

我对Android开发很新颖,但我可以告诉你在类似情况下对我有效。我定义我的布局如下:

<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 


    <EditText 
     android:id="@+id/outputText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:editable="false" /> 

    <Spinner 
     android:id="@+id/outputSpinner" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:prompt="@string/OutputBaseOptionsPrompt" /> 
</LinearLayout> 

我有两个项目的水平布局。 LinearLayout的宽度为“match_parent”,因此宽度与屏幕一样宽。在布局两个项目有以下:

 android:layout_width="0dp" 
     android:layout_weight="1" 

由于两个项具有1的layout_weight,它们将被以相同的宽度绘制。在这种情况下,每个项目占用可用空间的一半。如果您将其中一个项目的重量更改为“2”,则其宽度将为重量为“1”的项目的两倍。

+0

为什么给孩子一个0dp的layout_width而不是fill_parent/match_parent? – poitroae

+0

正如我所说,我对Android开发非常陌生。我正在尝试一些事情来使布局看起来很正确。我不知道我是否在某个地方读过这本书,或者我碰巧试过它。我只是改变了fill_parent,我仍然得到了预期的结果。 –

+0

好的,谢谢!我用重量解决了它,效果非常好! :) – poitroae

1

你已经有XML,使得它在一个屏幕尺寸工作?如果是这样,发布你到目前为止。

我会建议使用RelativeLayout作为你的根目录。您可以使用alignCenter属性将您的孩子移向中间。那么你只需要硬编码内边距(你想要多少按钮)而不是从你自己到边墙的边距。

您还可以避免必须通过制作自己的按钮9补丁图像来硬编码内边距。您可以在图像中添加一个透明像素边框来表示边距。您可能仍然希望为您希望支持的每种密度提供图像。

+0

谢谢您的回答把三个图像具有相同名称在华电国际MDPI和LDPI文件夹。我不能/不想使用9-patch-images - 按钮可绘制的是android形状:/。要么,这似乎是一个糟糕的黑客这样做。我喜欢边缘的想法,这就是我基本上做的事情。我已经添加了上面的代码,请看看它! – poitroae

0

解决的办法是你不使用硬编码值的任何地方 在绘图资源

的运行代码