将文本和图像放在同一行

时间:2015-11-10 20:16:47

标签: android eclipse

我试图将文字和图片放在同一条线上,但由于某种原因,它无法正常工作。文字很好,但没有图像的迹象。

<LinearLayout
    android:id="@+id/list_item3"
    android:layout_width="match_parent"
    android:layout_height="42dp"
    android:layout_below="@+id/list_item"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:background="#DCDBDB" >

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.00"
        android:text="@string/quoteDevil"
        android:textSize="21sp" />

    <ImageView
        android:layout_width="33dp"
        android:layout_height="match_parent"
        android:layout_marginRight="15dp"
        android:src="@drawable/nextarrow" />
</LinearLayout>

4 个答案:

答案 0 :(得分:4)

仅供参考:有一个“提示”用于在TextView内显示图像。

事实上,使用drawable / TextViewandroid:drawableRight文件夹中存储的所有图片实际上都可以嵌入android:drawableLeft与文本相关的几个关键位置和android:drawablePadding属性。

一个例子:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"     
    android:gravity="center"
    android:text="@string/my_contacts"
    android:drawableRight="@drawable/ic_action_add_group"
    android:drawablePadding="8dp"
/>

结果是:

Image within TextView

来源:Link

答案 1 :(得分:3)

在一行中包含两件事的最佳方法是使用TableRow

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/list_item"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true">
<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/TextView03"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/quoteDevil"
        android:textSize="21sp" />    
    <ImageView
        android:layout_width="33dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginRight="15dp"
        android:src="@drawable/nextarrow" />
</TableRow>
</LinearLayout>

但是如果你想使用LinearLayout,你必须将方向定义为水平

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/list_item"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true">
    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/quoteDevil"
        android:textSize="21sp" />
    <ImageView
        android:layout_width="33dp"
        android:layout_height="match_parent"
        android:layout_marginRight="15dp"
        android:src="@drawable/nextarrow" />
</LinearLayout>

答案 2 :(得分:1)

你需要为LinearLayout添加orientation属性:

android:orientation="horizontal"

并将width属性从inside_parent的内部元素更改为wrap_content,如果你将match_parent只显示一个元素

更改

android:layout_width="match_parent"

有关

android:layout_width="wrap_content"

实施例

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

 <TextView
    android:id="@+id/TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

答案 3 :(得分:0)

在RelativeLayout中

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="80dp"
        android:layout_height="80dp"
        app:srcCompat="@drawable/centra_crm" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/imageView2"
        android:padding="5dp"
        android:singleLine="true"
        android:text="Title" />

</RelativeLayout>