1

我正在制作一个随机食谱收集应用程序。现在我遇到了一个问题:目前我在设计它时考虑了手机的分辨率。但是,如果该应用程序用于具有更大分辨率的设备,例如。平板电脑。

我想要实现的是我希望按钮根据分辨率缩小。例如:当我垂直握住我的手机时,有 2 列按钮。当水平握住它时,仍然有 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>
4

3 回答 3

2

Android 有一些机制来处理这种事情。对于大多数人来说,只需为不同类别的设备设置不同的布局就足够了。IE:

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 会自动在您的应用加载的设备上选择正确的布局。有关详细信息,请参阅开发人员指南。或者,您可能需要定义自己的自定义视图,根据可用大小调整网格大小。这方面的一个例子是CellLayout,一个为 Launcher 中的应用程序网格编写的类

于 2013-07-01T22:28:02.683 回答
1

我实际上建议您使用 GridView 模式。

https://developer.android.com/guide/topics/ui/layout/gridview.html

好处包括

  • 如果您需要多行/多列,可以轻松自定义
  • 更快的性能,因为您重复使用了很多相同的视图。缩放比滚动视图方法好得多

这里的例子有两列电话

这里以平板电脑的四列为例

全部仅更改网格应显示的列数。根据“JRaymond”的建议,最好的方法是根据平板电脑/手机设置多个值来响应。例子

  • ->值->attrs_arin_view.xml
  • ->价值观-土地->attrs_arin_view.xml
  • -> 值-sw600dp -> attrs_arin_view.xml

内部值->attrs_arin_view.xml

   <resources>
        <integer name="number_of_column">2</integer>
    </resources>

然后将值域更改为 number_of_column 为 4。

然后在你的xml中你提到整数一次

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:horizontalSpacing="8dp"
        android:numColumns="@integer/number_of_column"
        android:padding="8dp"
        tools:listitem="@layout/grid_cell_note"
         >
    </GridView>
</LinearLayout>
于 2013-07-01T22:34:47.597 回答
1

您需要为不同的屏幕尺寸创建 xml 文件

http://developer.android.com/guide/practices/screens_support.html

设计您的 XML 文件以使用引用而不是硬编码字符串。然后,您可以分配对布局、按钮等的引用,这些引用根据屏幕尺寸具有不同的值。(使用android:padding="@dimen/pagepadding"代替android:padding="16dp"并在 values/dimens.xml 中定义 dp,如下所示<dimen name="pagepadding">16dp</dimen>:)

为此,您必须在项目中创建新文件夹,例如values-sw600dp最小宽度为 600dp 的设备(我相信像 Nexus 7)或values-sw720dp-land横向最小宽度为 720dp(我相信是 10 英寸平板电脑)的设备。

为此在开发者页面和互联网上做一些阅读。这不是太难

于 2013-07-01T22:20:27.680 回答