LinearLayout - 如何让文本显示在图标右侧?

3
作为一个android的新手,我只在上面工作了几天,但即使我已经搜索过所有可能的信息,我仍然卡住了,似乎没有人能够帮助我。到目前为止,我已经有了这个:http://img263.imageshack.us/i/sellscreen.jpg。我该如何将文字移到每个图标的旁边而不是它的下面?希望画廊也不会被移动。以下是我已经编写的代码:
<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

    <ImageView 
     android:id="@+id/test_image"
     android:src="@drawable/icon"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

    <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="The offcial UK driving theory test application. Over 190 questions."
     />

    <ImageView 
     android:id="@+id/test_image"
     android:src="@drawable/icon"

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

    <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="The offcial UK driving theory test application. Over 190 questions."/>

    <ImageView 
     android:id="@+id/test_image"
     android:src="@drawable/icon"

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

    <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="The offcial UK driving theory test application. Over 190 questions."/>

    <ImageView 
     android:id="@+id/test_image"
     android:src="@drawable/icon"

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

    <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="The offcial UK driving theory test application. Over 190 questions."/>

    <ImageView 
     android:id="@+id/test_image"
     android:src="@drawable/icon"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     />

    <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="The offcial UK driving theory test application. Over 190 questions."
        />

    </LinearLayout>

</ScrollView>

我的代码上半部分似乎无法显示,但那只是线性布局的开头。 我将永远感激任何能够帮助我的人,我已经苦思冥想了几天,却毫无进展。这真的让我很紧张。提前致谢!

2个回答

3
您需要在每个ImageView/TextView配对中使用LinearLayout包装。
<LinearLayout 
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
    <ImageView 
     android:id="@+id/test_image"
     android:src="@drawable/icon"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

    <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="The offcial UK driving theory test application. Over 190 questions."
     />
</LinearLayout>

很明显你没有向任何了解Android的人询问,因为这很简单。

我认为人们只是懒得看,因为我在两个安卓论坛上都发布了它。哦,好吧,我费尽心思地想了很久,结果问题在一秒钟内就解决了,非常非常感谢! - RED_
1
@RED_: 当然可以。但是也要考虑@Mayra的回答。我不确定在这种特定情况下使用相对布局是否更有效,但通常最好使用相对布局而不是深度嵌套线性布局。 - Falmarri
是的,RelativeLayout目前让我非常困惑。如何将所有内容放置在正确的位置是最重要的事情,但我相信随着经验的积累,我会掌握它的。 - RED_
相对布局的关键在于它们按照声明的顺序绘制。因此,如果您的相对布局中的第一件事具有一个属性的fill_parent,它将填充父级,而不管剩下什么。但是,如果您首先放置一个按钮或文本视图,然后再放置一个具有fill_parent的项目,它将填充剩余空间而不是全部空间。 - Falmarri

1

你有没有阅读过Android布局教程?开发者网站上有很多内容,比如声明布局常见布局对象hello views教程。

根据你的目标,有许多方法可以实现这一点。

如果是一个未知大小的项目列表,可能需要滚动吗?使用ListView。您可以为ListView提供自定义布局,将您的项目并排放置。

如果您有一组有限的项目,请使用RelativeLayout。告诉每个TextView在上面的那个下面布局,并且每个ImageView在其TextView右侧布局。

您也可以使用嵌套的LinearLayouts来实现这一点,其中一个是垂直的,另一个是水平的,但这比使用RelativeLayout要不高效。


你确定吗?我并不反对,但使用RelativeLayout比这种方法效率要低。一个嵌套了3个布局的视图结构并不算太复杂。 - Falmarri
1
当你处理那些运行于有限电力和电池供电的设备时,为什么要采用缓慢的方式执行任务,而不是尽可能地提高工作效率呢?如果一个复杂的操作可以节省几个计算周期,那还值得一试。但使用相对布局并不难。 - Vinay Pai
它肯定不太高效,但我不知道具体有多少影响。Commonsware曾经发表过一篇关于此的大型文章,但我现在找不到了,不过这篇文章展示了基本的原因:http://developer.android.com/resources/articles/layout-tricks-efficiency.html - Cheryl Simon
@vinay Pai:你是在和我说话还是和Mayra说话?你所说的比什么不高效?而且你有什么依据来支持这个说法? - Falmarri
我同意Mayra的观点,因为他/她发布了完全相同的文章(实际上我正在尝试重新找到它并注意到最近的评论)。我强烈怀疑差异非常大...但正如我所说...为什么要做一些稍微慢一点的事情,当你可以轻松地做一些更快的事情呢? - Vinay Pai
谢谢回复,我知道RelativeLayout在阅读教程后更有效率,但我仍在学习它(主要是如何定位所有内容),所以现在我将坚持使用LinearLayout。这是一个非常小的程序,所以我怀疑它会产生影响。我是一个新手,我会先掌握基础知识再继续前进。 - RED_

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接