开发者

Changing shape of textview in Android framework

开发者 https://www.devze.com 2023-02-25 11:44 出处:网络
I am working on android frameworks. I need to modify the shape of the textvi开发者_开发百科ew such that it should have rounded corners. Can you please help me with this issue?XML file saved at res/dra

I am working on android frameworks. I need to modify the shape of the textvi开发者_开发百科ew such that it should have rounded corners. Can you please help me with this issue?


XML file saved at res/drawable/gradient_box.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

This layout XML applies the shape drawable to a View:

<TextView
    android:background="@drawable/gradient_box"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

This application code gets the shape drawable and applies it to a View: Resources res = getResources(); Drawable shape = res. getDrawable(R.drawable.gradient_box);

TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);

Copy&Paste from http://developer.android.com/guide/topics/resources/drawable-resource.html, first result when you google "android textView rounded corners"

[EDIT]

As you want to change the source of the android framework, you can alter the style of the TextView by altering the default Drawables. In the sources the various Drawables are grouped in an inner class called Drawables:

class Drawables {
        final Rect mCompoundRect = new Rect();
        Drawable mDrawableTop, mDrawableBottom, mDrawableLeft, mDrawableRight;
        int mDrawableSizeTop, mDrawableSizeBottom, mDrawableSizeLeft, mDrawableSizeRight;
        int mDrawableWidthTop, mDrawableWidthBottom, mDrawableHeightLeft, mDrawableHeightRight;
        int mDrawablePadding;
    }

Just alter these Drawables as you like and you have redefined TextView.

I'm referring to that version of the TextView sources.

0

精彩评论

暂无评论...
验证码 换一张
取 消