如何根据屏幕尺寸调整六个按钮?

时间:2012-04-20 16:31:51

标签: java android android-layout

我正在为自己创建一个停车场应用程序,我有6个按钮,允许我为应用程序放置快捷方式。我为横向和纵向模式设计了一个布局。我在纵向模式下遇到屏幕尺寸问题。我的图标排列如下:

Text up here
------------
| 1  |  2  |
------------
| 3  |  4  |
------------
| 5  |  6  |
------------

以下是按钮的XML:

<Button
    android:id="@+id/btnLaunchSlotOne"
    style="@style/launchButton"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_weight="1"
    android:layout_marginRight="8dp"
    android:padding="10dp"
    android:text="Navigation"
    android:textColor="#ffffff" />

现在我遇到的问题是当我在较小的屏幕尺寸上测试应用程序时,按钮似乎没有调整大小,而是从底部的屏幕上移开。我怎样才能使它们正确扩展?我以为用“dp”照顾了所有这些?

我是否需要以编程方式计算屏幕尺寸?

感谢您的建议/帮助!

4 个答案:

答案 0 :(得分:3)

使用类似下面的布局来正确嵌套按钮:

<LinearLayout
    android:orientation="vertical">
    <TextView>Put your Text here</TextView>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_weightSum="2">
        <LinearLayout
            android:orientation="vertical"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weightSum="3"
            android:layout_weight="1">
            <Button 
                android:layout_weight="1"/>
            <Button 
                android:layout_weight="1"/>
            <Button 
                android:layout_weight="1"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_weightSum="3"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1">
            <Button 
                android:layout_weight="1"/>
            <Button 
                android:layout_weight="1"/>
            <Button 
                android:layout_weight="1"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

然后每个按钮需要高度为0dp(这样可以应用权重并水平缩放按钮)和填充父级的宽度。

诀窍在于,子元素的权重将导致设置为0dp的维度填充到父元素的weightSum的百分比。

例如,我有一个线性布局,其中weightSum 3和3个子按钮的重量为1.当高度设置为0dp时,它们各自占据父级高度的1/3。当width设置为0dp时,它们将各自占父项宽度的1/3。

这是根据百分比宽度或高度组织项目的最佳方式。

另外如果你有两个按钮,其中一个重量为2,另一个按重量为1,那么1个按钮将是父母身高/宽度的33%,另一个是父母身高/宽度的66%。方便的小技巧,适用于所有屏幕。

答案 1 :(得分:2)

如果您希望按钮始终占据整个屏幕,您可以使用GridView,它会自动处理所有屏幕尺寸的缩放。

答案 2 :(得分:0)

您可以使用以下xml文件:::

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView 
 android:text="Text 1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 /> 
 <LinearLayout 
  android:orientation="horizantal" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weight_sum = "2">
 <Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
<Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
</LinearLayout>
 <LinearLayout 
  android:orientation="horizantal" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weight_sum = "2">
 <Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
<Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
</LinearLayout>
 <LinearLayout 
  android:orientation="horizantal" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weight_sum = "2">
 <Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
<Button android:layout_width="fill_parent" android:layout_weight="1"
  android:layout_height="fill_parent" android:text="test" />
</LinearLayout>


</LinearLayout>

答案 3 :(得分:0)

查看示例here.特别是Dashboard Layout

相关问题