How to create a custom textview in Android?

I'm developing layout. I developed drawable for this layout. But the text cannot be adjusted in this view. Please provide me with any other guidance on the library. Thank you

resolvent:

In Android, there is a method called SetRotation (float), which you can use

textview.setRotation(float);

Therefore, if you want to support it, you can use it

if (Build.VERSION.SDK_INT < 11) {

    RotateAnimation animation = new RotateAnimation(oldAngel, newAngel);
    animation.setDuration(100);
    animation.setFillAfter(true);
    watermarkText.startAnimation(animation);
} else {

    watermarkText.setRotation(progress);
}

Editor: This is my solution:

This is my complete activity_ main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tool="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary">

    <RelativeLayout
        android:id="@+id/item"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#FFFFFFFF"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:textColor="#FFFFFFFF"
            android:text="HP  EliteBook 1212x"
            android:textAlignment="gravity"
            android:gravity="center"
            android:background="@color/colorPrimaryDark"

            />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_margin="0dp"
            android:src="@drawable/laptop"/>

        <ImageView
            android:layout_width="175dp"
            android:layout_height="175dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_margin="0dp"
            android:padding="0dp"
            android:src="@drawable/triangle"/>

        <LinearLayout
            android:id="@+id/prices"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:orientation="vertical"
            android:padding="7dp"
            android:rotation="-45.0"
            android:textAlignment="center">

            <TextView
                android:id="@+id/old_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="200$"
                android:textColor="#FFFFFFFF"
                />

            <TextView
                android:id="@+id/new_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/old_price"
                android:layout_gravity="center"
                android:text="400$"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#FFFFFFFF"/>
        </LinearLayout>


    </RelativeLayout>

</RelativeLayout>

To create a triangle, create the triangle.xml file in the following directory:

your_app_name/app/src/main/res/drawable

The contents are as follows:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <rotate
            android:fromdegrees="-45"
            android:todegrees="0"
            android:pivotX="150%"
            android:pivotY="20%" >
            <shape
                android:shape="rectangle" >
                <solid android:color="#ff0000" />
            </shape>
        </rotate>
    </item>
</layer-list>

In your Java mainactivity class:

    TextView oldPrice = (TextView) findViewById(R.id.old_price);
    oldPrice.setPaintFlags(oldPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

Add a strikethrough effect to your old price text

If you have any questions, please feel free to ask. I hope it will be helpful to you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>