如何定义由两个图像包围的textview?

时间:2013-05-08 17:11:40

标签: android

textview应填充图片留下的空间:

[imageview 40px, aligned left][textview][imageview 40px, aligned right]

这是我的代码:

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

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/indicator_code_lock_point_area_green_holo"
        android:tag="heart" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:text="Shakespeare was a respected poet and playwright in his own day, but his reputation did not rise to its present heights until the 19th century." />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/indicator_code_lock_point_area_green_holo"
        android:tag="heart" />

</LinearLayout>

正如您所看到的,文本会占据右侧的图像。

4 个答案:

答案 0 :(得分:0)

使用drawableLeftdrawableRight属性。

答案 1 :(得分:0)

在XML中为文本视图写这个

<TextView
        android:id="@+id/TextView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawableLeft="@android:drawable/ic_menu_call"
        android:drawableRight="@android:drawable/ic_menu_call"
        android:gravity="center_horizontal|center_vertical"
        android:minHeight="100dp"
        android:padding="20dp"
        android:text="Contacts"/>

下面,

android:drawableLeft="@android:drawable/ic_menu_call"
android:drawableRight="@android:drawable/ic_menu_call"

设置图像

答案 2 :(得分:0)

如果您使用LinearLayout,则只需为weight=1设置width=0dpTextView

<TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:text="Shakespeare was a respected poet and playwright in his own day, but his reputation did not rise to its present heights until the 19th century." />

但是如果您没有为ImageView设置ID,并且ImageView的抽签不是9.png answer of Anupam Majhi则更好(您不需要使用ImageView那样)。

答案 3 :(得分:0)

您可以尝试这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="Your Text Goes Here" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>