如何使布局增长和缩小?

时间:2013-07-01 22:07:49

标签: android xml layout

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

我想要实现的是我希望按钮根据分辨率而缩小。例如:垂直拿着手机时,有2列按钮。水平握住时,仍有2列,但视野变宽。然后应该有4列填充尽可能多的空白区域。

两张图片来说明我的想法:

vertical 垂直

horizontal 水平,另外两列

我的代码:

<?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>

3 个答案:

答案 0 :(得分:2)

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。或者,您可能需要定义自己的自定义视图,以根据可用大小调整网格大小。一个例子是CellLayout,一个为Launcher中的应用程序网格编写的类

答案 1 :(得分: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,用于宽度最小为600d的设备(如我相信的Nexus 7)或values-sw720dp-land,用于宽度最小为720dp的设备(10在景观中我相信的英寸平板电脑。

在开发者页面和互联网上阅读。这不是太困难

答案 2 :(得分:1)

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

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

Benfits包括

  • 如果您需要多行/列,可以轻松自定义
  • 更快的性能,因为您重复使用了大量相同的视图。比scrollview方法更好地扩展

Here example with two column for Phones

Here example with four column for tablets

全部仅更改网格应显示的列数。通过“JRaymond”推荐的最佳方式,通过具有多个值resposnes取决于它是否是平板电脑/手机。实施例

  • - &gt; values - &gt; attrs_arin_view.xml
  • - &GT;值-陆地&GT; attrs_arin_view.xml
  • - &gt; values-sw600dp - &gt; attrs_arin_view.xml

内部值 - &gt; attrs_arin_view.xml

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

然后更改values-land以使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>
相关问题