我正在尝试制作一个随机食谱收集应用程序。 现在我遇到了一个问题:目前我在设计它时考虑到了手机的分辨率。但是,如果该应用程序是在分辨率更大的设备上使用的,例如。平板电脑。
我想要实现的是我希望按钮根据分辨率变大变小。例如:当我竖着拿手机时,有两列按钮。水平握住它时,仍然有 2 列,但视野变宽了。然后应该有 4 列来填充尽可能多的空白区域。
两张图说明我的想法:
垂直
水平多了 2 列
我的代码:
<?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="#ffcc33"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1000" >
<EditText
android:id="@+id/search_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1000"
android:ems="5"
android:hint="@string/search_hint" />
<Button
android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/search_button" />
</LinearLayout>
<ScrollView android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<RelativeLayout
android:layout_width="225dp"
android:layout_height="wrap_content"
android:layout_below="@+id/search_box"
android:layout_gravity="center"
android:layout_marginTop="15dp" >
<Button
android:id="@+id/btn_lihatoidud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:drawableTop="@drawable/lihatoidud"
android:layout_alignParentTop="true"
android:text="Lihatoidud"
android:textSize="18sp" />
<Button
android:id="@+id/btn_kypsetised"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_lihatoidud"
android:layout_marginTop="15dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/kypsetised"
android:text="Küpsetised"
android:textSize="18sp" />
<Button
android:id="@+id/btn_seenetoidud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_kypsetised"
android:layout_marginTop="15dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/seenetoidud"
android:text="Seenetoidud"
android:textSize="18sp" />
<Button
android:id="@+id/btn_juustutoidud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_seenetoidud"
android:layout_marginTop="15dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/juustutoidud"
android:text="Juustutoidud"
android:textSize="18sp" />
<Button
android:id="@+id/btn_lisandid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_juustutoidud"
android:layout_marginTop="15dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/lisandid"
android:text="Lisandid"
android:textSize="18sp" />
<Button
android:id="@+id/btn_supid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@android:color/transparent"
android:drawableTop="@drawable/supid"
android:text="Supid"
android:textSize="18sp" />
<Button
android:id="@+id/btn_voileivad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/btn_supid"
android:layout_marginTop="15dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/voileivad"
android:text="Võileivad"
android:textSize="18sp" />
<Button
android:id="@+id/btn_pudrud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/btn_voileivad"
android:layout_marginTop="15dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/pudrud"
android:text="Pudrud"
android:textSize="18sp" />
<Button
android:id="@+id/btn_joogid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/btn_pudrud"
android:layout_marginTop="15dp"
android:background="@android:color/transparent"
android:drawableTop="@drawable/joogid"
android:text="Joogid"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
最佳答案
Android 有一些机制来处理这种事情。对于大多数人来说,简单地为不同类别的设备设置不同的布局就足够了。即:
res/
layout/
my_layout.xml
layout-land/ # landscape
my_layout.xml
layout-sw600dp # bigger devices
my_layout.xml
layout-sw600dp-land # Big and landscape
Android 会自动在您的应用加载的设备上选择正确的布局。查看Developer Guide那里有详细信息。或者,您可能需要定义自己的自定义 View ,根据可用大小调整网格大小。这方面的一个例子是 CellLayout
,为 Launcher 中的应用程序网格编写的类
关于android - 如何让布局变大变小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17414466/