简体   繁体   English

android-如何构建可以拉伸的复杂视图?

[英]android - How to build complex views that stretch?

I'm at the planning stages of my first proper android app. 我正在第一个适当的android应用的计划阶段。 Part of the brief is to have a very "good looking" interface (aka, plenty of graphics). 简要说明的一部分是拥有一个非常“漂亮”的界面(又名,大量的图形)。 As I'm a web developer, and I'm used to the CSS way of doing things, I'm struggling to understand how I will cater for all the different resolutions when building my layouts. 因为我是一名Web开发人员,并且已经习惯了CSS的处理方式,所以我一直在努力了解如何在构建布局时满足所有不同的分辨率。 To cut it short, my question is this: how do I build complex, image heavy views that can be seen in apps such as SoundHound (example view here, for those not familiar with this app: http://getandroidstuff.com/wp-content/uploads/2010/09/SoundHound-Android2.jpg )? 简而言之,我的问题是:我如何构建可以在诸如SoundHound之类的应用中看到的复杂的图像沉重视图(对于不熟悉该应用的用户,此处为示例视图: http : //getandroidstuff.com/wp -content / uploads / 2010/09 / SoundHound-Android2.jpg )? Is there a way I can tile images in a similar way to the way I'd tile in CSS? 有没有一种方法可以像我在CSS中进行平铺一样平铺图像?

It is important that you know how all Layouts Views work before you start designing your main layout. 在开始设计主布局之前,了解所有布局视图的工作方式非常重要。 Android OS by very design supports stretching of views and GroupViews. 精心设计的Android操作系统支持视图和GroupView的扩展。 You need to use dp (density independent pixel) units. 您需要使用dp(密度独立像素)单位。

In the example you provided, there are some nice nine patch PNGs. 在您提供的示例中,有一些不错的九个补丁PNG。 Check C:\\android-sdk-windows\\tools\\draw9patch.bat if on PC. 如果在PC上,请检查C:\\ android-sdk-windows \\ tools \\ draw9patch.bat。 These pngs stretch properly as backgrounds for any resolution. 这些png可以适当地拉伸为任何分辨率的背景。

Linear layout have weight which allows you determine how much space will each child occupy, similar to the old column width for tables in %. 线性布局具有权重,可让您确定每个子项将占用多少空间,类似于表格中旧列的宽度(以%为单位)。 Just like in old hml, to make a rich layout you use nested GroupViews (eg LinearLayout, RelativeLayout etc) and populate them with children views like ImageView, TextView and son on. 就像在旧的html中一样,要制作丰富的布局,请使用嵌套的GroupViews(例如LinearLayout,RelativeLayout等),并使用ImageView,TextView和sonon之类的子视图填充它们。

It is possible also to have text styled with <b>,<i> tags and have 也可以使用<b>,<i>标签对文本进行样式设置并具有

<img src="DrawableName">

see an example: Html.ImageGetter 查看示例: Html.ImageGetter

Opposite to a nested layout would be to use the RelativeLayout and determine the position of each child view by its top and left margin anywhere on the screen in relation to the top-left corner of their parent RelativeLayout. 与嵌套布局相对的是,使用RelativeLayout并通过相对于其父级RelativeLayout的左上角,在屏幕上的任意位置按其子视图的左上角和左边界确定每个子视图的位置。

Android has some conventions, that help you out when it comes to different screen sizes, screen densities, screen ratios ... Android有一些约定,可以帮助您解决不同的屏幕尺寸,屏幕密度,屏幕比例...

For example there is a directory called "drawable" where you can put the image resources for your application. 例如,有一个名为“ drawable”的目录,您可以在其中放置应用程序的图像资源。 However, if you want to ship your application for devices with different resolutions, you can create additional folders named "drawable-ldpi", "drawable-mdpi", "drawable-hdpi". 但是,如果要为具有不同分辨率的设备提供应用程序,则可以创建名为“ drawable-ldpi”,“ drawable-mdpi”,“ drawable-hdpi”的其他文件夹。 The ldpi folder contains image resources for low density screens, mdpi is meant for medium density and hdpi meant for high density. ldpi文件夹包含用于低密度屏幕的图像资源,mdpi用于中等密度,而hdpi用于高密度。 The folder names are part of the Android conventions. 文件夹名称是Android约定的一部分。 When loading an image resource at runtime eg on a high density device, the system is looking for the image resource in the drawable-hdpi folder. 在运行时(例如在高密度设备上)加载图像资源时,系统正在drawable-hdpi文件夹中寻找图像资源。 It there is no image with that name, the system will look in the drawable folder. 没有该名称的图像,系统将在drawable文件夹中查找。

This concept also applies to the xml layouts you define in your "layout" folder. 此概念也适用于您在“布局”文件夹中定义的xml布局。 Maybe you say that the layout homeScreen.xml needs to look different when the user switches from portrait to landscape mode. 也许您说当用户从纵向模式切换到横向模式时,布局homeScreen.xml需要看起来有所不同。 Then you can create a folder "layout-land". 然后,您可以创建一个文件夹“ layout-land”。 You create another homeScreen.xml in there and change its apperance to your needs. 您在其中创建另一个homeScreen.xml,并根据需要更改其外观。 At runtime the system detects the current mode. 在运行时,系统将检测当前模式。 If in landscape, it will first check if there ist a folder "layout-land" and if it contains the layout file with the correct name. 如果是横向,它将首先检查是否存在“ layout-land”文件夹,并且其中是否包含具有正确名称的布局文件。 If not, or when the user switches back in portrait mode and there is no folder "layout-port", the system looks for the layout file in the "layout" folder. 如果不是,或者当用户切换回纵向模式并且没有文件夹“ layout-port”时,系统会在“ layout”文件夹中查找布局文件。

This is only a small part of it, but I hope, you got a first glance and everything was understandable. 这只是其中的一小部分,但我希望您乍看之下一切都可以理解。 Further information can be found in the official refrence 更多信息可以在官方参考资料中找到

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

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