在Android中增加TextView下划线的高度 [英] Increase TextView underline height in android

查看:196
本文介绍了在Android中增加TextView下划线的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下划线创建了一个 TextView .它工作正常,但是我需要知道如何在 TextView 中增加下划线的高度.我检查了很多,但只显示了如何绘制下划线,而这里没有人介绍如何增加下划线的高度.

I created a TextView with underline. It's working perfectly but I need to know how to increase the height of the underline in the TextView. I checked many but it only shows how to draw underline, but no one here in how to increase size of height in underline.

有什么办法吗?

如何增加下划线高度?

推荐答案

您可以创建自定义视图.很简单您可以给下划线任何所需的厚度.

You can create a custom view. It's pretty easy. You can give the underline any thickness you want.

要创建customView:

To create a customView:

CustomView.java

package com.rachit.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

/**
 * Custom View
 * Created by Rachit on 18-Apr-16.
 */
public class CustomView extends View {

    // Label text
    private String viewText;
    // Underline Thickness
    private float underlineThickness;

    // Paint for drawing custom view
    private Paint viewPaint;

    public CustomView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        viewPaint = new Paint();
        // Get the attributes specified in attrs.xml using the name we included
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.CustomView, 0, 0);

        try {
            // Get the text and colors specified using the names in attrs.xml
            viewText = typedArray.getString(R.styleable.CustomView_viewText);
            underlineThickness = typedArray.getDimension(R.styleable.CustomView_underlineThickness, 1f);
        } finally {
            typedArray.recycle();
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // Draw the View

        // Drawing the text on the view
        viewPaint.setColor(Color.BLACK); // Set Text color to whatever you want
        viewPaint.setTextSize(50); // Set Text Size to whatever you want
        canvas.drawText(viewText, getX() + 5, getMeasuredHeight() / 2 + 20, viewPaint); // 5 and 20 are the left and top padding, you can customize that too.

        viewPaint.setColor(Color.BLACK); // Set Underline color to whatever you want
        canvas.drawRect(getX(), getMeasuredHeight() - underlineThickness, getX() + getMeasuredWidth(), getMeasuredHeight(),
                viewPaint); // Set the start and end points of the Underline

    }

    public String getViewText() {
        return viewText;
    }

    public void setViewText(String viewText) {
        this.viewText = viewText;
        invalidate();
        requestLayout();
    }

    public float getUnderlineThickness() {
        return underlineThickness;
    }

    public void setUnderlineThickness(float underlineThickness) {
        this.underlineThickness = underlineThickness;
        invalidate();
        requestLayout();
    }
}

res/values 文件夹中的

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">

        <attr name="viewText" format="string" />
        <attr name="underlineThickness" format="dimension" />

    </declare-styleable>
</resources>

现在,您可以像这样在XML中定义视图:

Now you can define your view in the XML like this:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.rachit.customview.MainActivity">

    <Button
        android:id="@+id/change"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Change View" />

    <com.rachit.customview.CustomView
        android:id="@+id/customView"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_marginTop="30dp"
        custom:underlineThickness="5dp"
        custom:viewText="My View" />
</LinearLayout>

您甚至可以通过编程方式控制View:

And you can even control the View programmatically:

MainActivity.java

package com.rachit.customview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    CustomView customView;
    Button change;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        customView = (CustomView) findViewById(R.id.customView);
        change = (Button) findViewById(R.id.change);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customView.setViewText("Wassup??");
                customView.setUnderlineThickness(1.0f);
            }
        });
    }
}

输出看起来像这样:

然后单击按钮,可以通过编程方式修改视图:

And on clicking the button, the view can be modified programmatically:

这篇关于在Android中增加TextView下划线的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆