首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

【Android Training - 01】适配不同的荧屏[Lesson 1-支持不同的屏幕大小]

2012-07-15 
【Android Training - 01】适配不同的屏幕[Lesson 1-支持不同的屏幕大小]Dependencies and prerequisites[前

【Android Training - 01】适配不同的屏幕[Lesson 1-支持不同的屏幕大小]

Dependencies and prerequisites[前提条件]

  • Android 1.6 or higher (2.1+ for the sample app)
  • Basic knowledge of?Activities?and?Fragment
  • Experience building an Android?User Interface
  • Several features require the use of the?support library

    You should also read
    • Supporting Multiple Screens

      Try it out
    • Use RelativeLayout
    • Use Size Qualifiers
    • Use the Smallest-width Qualifier
    • Use Layout Aliases
    • Use Orientation Qualifiers
    • Use Nine-patch Bitmaps

      Use "wrap_content" and "match_parent"[使用wrap_content与match_parent而不是hard-coded]
      If you use?"wrap_content", the width or height of the view is set to the minimum size necessary to fit the content within that view, while?"match_parent"?(also known as"fill_parent"?before API level 8) makes the component expand to match the size of its parent view.

      wrap_content:宽高根据内容大小调整
      match_parent:伸展至与父控件一致

      显然我们不能hard-coded(写死大小)

      Use RelativeLayout[使用相对布局][如果我们需要一个相对的位置而不是仅仅是直线型的布局,那么我们可以使用RelativeLayout]

      上面图片是:Screenshot on a QVGA screen (small screen).

      【Android Training - 01】适配不同的荧屏[Lesson 1-支持不同的屏幕大小]

      上面图片是:Screenshot on a WSVGA screen (large screen).

      Use Size Qualifiers[使用大小标识符][虽然我们可以用上面的方法定义布局文件,让其在不同的情况下进行拉伸等动作,可是在某些比较大的屏幕,比如平板与TV上面还是不太适合,我们最好可以在这种情况下使用两套不同的布局文件来适配大小,我们可以使用大小标示符来标记不同的布局,让机器在运行程序的时候根据自身的大小来选择显示哪个布局][通常在?Tablets and TVs的AP上使用"two?pane"的布局,这样]

      ?

      ?

      • res/layout/main.xml, single-pane (default) layout:
        ?single pane, with action bar
      • 7" tablet, landscape:?dual?pane, wide, with action bar
      • 10" tablet, portrait:?dual pane, narrow, with action bar
      • 10" tablet, landscape:?dual pane, wide, with action bar我们可以使用上面说的别名方法,对上面的需求抽取出一些公共的布局元素,定义下面几个布局:

        res/layout/onepane.xml:
        res/layout/onepane_with_bar.xml:
        res/layout/twopanes.xml:
        res/layout/twopanes_narrow.xml:

        ?

        这样以来,我们需要在setContentView的时候选择main_layout就可以了,系统会选择到相应的Value里面取出对应的布局文件进行显示

        ?

        ?

          res/values/layouts.xml:


          可以看到这样的图片不会对那个下拉图标进行拉伸,这样就达到了我们需要的效果,不然下拉图标也被拉伸会显得很不合适。


热点排行