安卓:我应该使用哪种视图用于显示文字和图片?视图、哪种、文字、图片

2023-09-07 13:19:25 作者:天真丿微笑丶

我的应用程序显示一个项目列表,每一行是一个项目标题,形象的旁白,该行保留的文本空间70%,而图像的30%。试想一下,应用程序商店的外观iphone等。建议用于此目的的视图/布局组合?

My app shows a list of items, where each line is an item title with its image asides, the line reserves 70% of space for text and 30% for image. Imagine what iphone app store looks like. Which view/layout combo is recommended for this purpose?

我用Google搜索,发现这篇文章:

I googled and find this article:

HTTP://www.curious-creature .ORG / 2009/02/22 / Android的布局技巧-1 /

请问这个观点听起来好?

Does this view sound good?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"

android:padding="6dip">

<ImageView
    android:id="@+id/icon"

    android:layout_width="wrap_content"
    android:layout_height="fill_parent"

    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginRight="6dip"

    android:src="@drawable/icon" />

<TextView
    android:id="@+id/secondLine"

    android:layout_width="fill_parent"
    android:layout_height="26dip" 

    android:layout_toRightOf="@id/icon"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"

    android:singleLine="true"
    android:ellipsize="marquee"
    android:text="Simple application that shows how to use RelativeLayout" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:layout_toRightOf="@id/icon"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_above="@id/secondLine"
    android:layout_alignWithParentIfMissing="true"

    android:gravity="center_vertical"
    android:text="My Application" />

推荐答案

如果通过了​​iPhone应用程序商店的样子,你的意思是pretty多的每一个列表那里(嗯,至少据我的判断说明,从未真正见过的iPhone应用程序商店),但似乎只有一个工作方式。 理想的解决方案是使用 RelativeLayout的为,可以让你有两行文本旁边的图像,而无需复杂的布局结构。然而,正如你可以在this问题和this另一个问题,似乎有一些问题,这一点。所以,回退是使用水平的LinearLayout和窝第二竖直的LinearLayout中,如果你需要多行文本。看到这些帖子关于你的LinearLayout应该是什么样子的更多细节。

If by the "what the iPhone app store looks like", you mean "pretty much every single list out there" (well, at least as far as I can judge by the description, having never actually seen the iPhone app store), there seems to be only one way which works. The 'ideal' solution would be to use a RelativeLayout as that allows you to have two lines of text next to the image without needing to a complex layout structure. However, as you can see in this question and this other question, there seems to be some issue with that. So, the fallback is to use a horizontal LinearLayout, and nest a second vertical LinearLayout in it if you need multiple lines of text. See those posts for more details about what your LinearLayout should look like.

请注意:我假设你知道如何使用一个ListView。如果你不知道如何列表视图的工作,那么我建议这文章,作为一个很好的起点。这是一个有点老了,但还是不错的。只要确保你阅读它的续集的为好,这解释了如何使用convertView优化。

Note: I'm assuming you know how to use a ListView. If you don't know how ListViews work, then I recommend this article as a good starting place. It's a bit old, but still good. Just make sure you read its sequels as well, which explain how to use the convertView optimisation.

编辑:对于任何人读这篇文章,这里是一个名单上的教程,更好的格式,使其更容易阅读。

For anyone else reading this, here is a tutorial on Lists with much better formatting, making it easier to read.