如何创建带有图像和边框的textview,如附加的图像

时间:2015-09-23 12:22:43

标签: android css xml

我想创建一个类似下图的文本视图。我试过但不能像这种格式。

enter image description here

这是我的xml文件。

<View android:background="#ff340c1c"
    android:layout_width="fill_parent"
    android:layout_height="0.5dip"
    android:layout_above="@+id/genere"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/genere"
    android:text="Search by Movie Genere"
    android:layout_above="@+id/rating"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="78dp"
    android:textSize="20sp"/>

<View android:background="#ff340c1c"
    android:layout_width="fill_parent"
    android:layout_height="0.5dip"
    android:layout_alignBottom="@+id/genere"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

3 个答案:

答案 0 :(得分:3)

使用带边框的自定义背景和drawableLeft -

抽拉/ my_rect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <stroke
        android:width="0.5dp"
        android:color="@color/gray" />

    <solid android:color="@color/white" />

    <corners android:radius="0px" />

</shape>

在XML中:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/genere"
    android:text="Search by Movie Genere"
    android:layout_above="@+id/rating"
    android:drawableLeft="@drawable/someimage" // ADD
    android:background="@drawable/my_rect"     // ADD
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="78dp"
    android:textSize="20sp"/>

答案 1 :(得分:3)

注意:存在许多可能的解决方案。

可以使用简单的线性布局作为边框来完成。

 <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#EEEEEE"
            android:orientation="horizontal"
            android:padding="1dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#FFFFFF"
                android:orientation="horizontal" >

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

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Search By Venue" />
            </LinearLayout>
        </LinearLayout>

答案 2 :(得分:0)

要将图像添加到TextView,请参阅android:drawableLeft。要添加边框,您可以查看9-patch图像。如上所述生成9补丁图像,并将其设置为TextView的背景。