TextView---自定义样式

有时候需要自定义一个按钮,用textview也可以实现
(1)创建一个供TextView使用的背景selector并且放到drawable文件夹下面

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <!-- 填充的颜色 -->
            <solid android:color="@android:color/white"></solid>
            <!-- android:radius 弧形的半径 -->
            <corners android:radius="6px" />
            <!-- 绘制边框-->
            <stroke android:width="2px" android:color="#2c72ac" android:dashGap="0dp" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <corners android:radius="6px" />
            <stroke android:width="2px" android:color="#3c99e5" android:dashGap="0dp" />
        </shape>
    </item>
</selector>

(2)创建一个供TextView使用的动态改变字体颜色的selector并且放到color文件夹(没有可以在res文件夹下新建color文件夹)下面:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue_3C99E5" android:state_focused="true"></item>
    <item android:color="@color/blue_3C99E5" android:state_pressed="true"></item>
    <item android:color="@color/blue_CCCCCC"></item>

</selector>

注意:item是从上往下匹配的,如果匹配到一个item那它就将采用这个item,所以设置默认的状态,一定要写在最后,如果写在前面,则后面所有的item都不会起作用
(3)在xml中调用:

<TextView
android:id="@+id/famousperson_listview_tv_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关注"      
android:drawableLeft="@drawable/famousperson_add_like_drawableleft"
android:paddingTop="6dip"
android:paddingBottom="6dip"
android:paddingLeft="9dip"
android:paddingRight="9dip"
android:textSize="12sp"
android:textColor="@color/famousperson_tv_eachother_color"
android:background="@drawable/famousperson_button_bg_liked" />

(4)在代码中调用:

viewHolder.tv_like.setBackgroundResource(R.drawable.famousperson_button_bg_haslike);
viewHolder.tv_like.setCompoundDrawables(viewHolder.dw_addlike, null, null, null);
viewHolder.tv_like.setText("关注");
ColorStateList csl = context.getResources().getColorStateList(R.color.famousperson_tv_like_color);
viewHolder.tv_like.setTextColor(csl);
Android中,TextView 是用于显示文本的组件。`text-indent` 属性并不是原生支持的HTML样式属性,在纯Android布局XML文件中,TextView本身并不直接提供文本缩进的功能。然而,如果你需要类似的效果,可以借助第三方库,比如 Android Custom View 或者自定义 TextView样式。 如果你想通过代码动态设置文本的首行缩进,你可以创建一个自定义的 `MyTextView` 类并覆盖 `onDraw()` 方法,然后在绘制文本前手动调整文本框的位置。例如: ```java class MyTextView extends TextView { private int indent; public MyTextView(Context context) { super(context); } // 添加这个构造函数接受初始缩进值 public MyTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); setIndentFromAttribute(attrs); } public void setIndent(int value) { indent = value; } private void setIndentFromAttribute(AttributeSet attrs) { if (attrs != null && attrs.getAttributeValue(R.styleable.MyTextView_indent, null) != null) { indent = Integer.parseInt(attrs.getAttributeValue(R.styleable.MyTextView_indent, "0")); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.translate(indent, 0); // 移动画布开始位置进行缩进 drawText(getText(), getCompoundPaddingLeft(), getTop(), getPaint()); } } ``` 然后在XML布局文件中引用这个自定义的 `MyTextView`,并设置 `indent` 属性: ```xml <com.example.YourPackage.MyTextView android:id="@+id/text_view" android:text="这是有缩进的文本..." app:indent="36" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值