繁体   English   中英

如何获得六个按钮以根据屏幕尺寸进行调整?

[英]How do I get six buttons to resize based on the screen size?

我正在为自己创建一个汽车停靠应用程序,我有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”可以解决所有这些问题?

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

感谢您的建议/帮助!

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

<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的百分比。

例如,我有一个权重为3的线性布局,三个权重为1的子按钮。当height设置为0dp时,它们各自将以父级高度的1/3作为高度。 当width设置为0dp时,它们各自将采用父级宽度的1/3。

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

顺便说一句,如果您有两个按钮,其中一个按钮的权重为2,另一个按钮的权重为1,则1个按钮的权重为父级高度/宽度的33%,其他为父级高度/宽度的66%。 方便的小技巧,可在所有屏幕上使用。

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

您可以使用以下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>

在此处查看示例 特别是仪表板布局

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM