Android TextView带背景图片和自定义边框

本文介绍如何在Android中使用ConstraintLayout和layer-list自定义TextView的样式,包括背景图片、边框和定位。通过XML布局文件实现复杂的UI设计,如设置TextView在上下比0.7的位置,并应用自定义的item_main样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TextView带背景图片和自定义边框,效果如下图:

页面xml:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/icon_main_title"
        app:layout_constraintTop_toTopOf="parent"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_course"
        android:background="@drawable/item_main"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tv_label"
        app:layout_constraintVertical_bias="0.7"/>
</android.support.constraint.ConstraintLayout>

app:layout_constraintVertical_bias="0.7"是TextView位于上下比0.7的位置。

item_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white"/>
            <stroke
                android:color="@color/itemColor"
                android:width="1dp"/>
            <padding
                android:top="@dimen/normal_match_20"
                android:bottom="@dimen/normal_match_20"
                android:left="@dimen/normal_match_10"
                android:right="@dimen/normal_match_10"/>
            <corners android:radius="@dimen/normal_corner_radius"/>
        </shape>
    </item>
    <item>
        <bitmap android:src="@drawable/ic_launcher"/>
    </item>
</layer-list>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值