【问题标题】:How to make custom textview in android? [closed]如何在android中制作自定义文本视图? [关闭]
【发布时间】:2016-04-03 06:24:18
【问题描述】:

我正在开发布局。我为此布局开发了可绘制对象。但此视图中的文本未调整。任何人告诉我有关库的任何其他指导方针,请提供给我。谢谢

【问题讨论】:

    标签: android android-layout android-fragments textview


    【解决方案1】:

    在Android中有一个方法setRotation(float),你可以使用它

    textview.setRotation(float);
    

    注意:此方法是在 API 11

    中添加的

    所以如果你想支持它,你可以使用它

    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);
    }
    

    编辑:这是我的解决方案:

    这是我完整的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>
    

    要创建三角形,请在此目录中创建triangle.xml 文件:

    your_app_name/app/src/main/res/drawable
    

    并作为内容放置此代码:

    <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>
    

    在您的 Java MainActivity 类中放置:

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

    为您的旧价格文本添加删除线效果。

    如果您有任何问题,请随时提出。 希望对你有帮助

    【讨论】:

    • 1543 谢谢..但文本不是动画我的要求..我的要求是文本是固定的..
    • 我已经添加了我的解决方案,希望对您有所帮助
    • 谢谢兄弟..它正在工作....
    【解决方案2】:

    TextView 中有属性。您可以根据您的要求旋转 请参考以下示例以供参考

    <TextView
                    android:id="@+id/won_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:paddingTop="20dp"
                    android:rotation="-45"
                    android:text="@string/won"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@android:color/white"
                    android:textSize="34sp" />
    

    【讨论】:

      【解决方案3】:

      最简单的方法是:

       <TextView
              android:id="@+id/rotated"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_alignParentRight="true"
              android:background="#AA000F"
              android:rotation="-50"
              android:text="ABC"
              android:textColor="#44CC44"
              android:textSize="32sp" />
      

      您也可以使用其他布局或编辑文本来做到这一点

      【讨论】:

      • 这是没有动画的...简单如你所愿
      • 我知道,但根据我的问题没有回答..
      • 你必须为这种类型的文本使用标签
      • 这会使文本完全按照您的意愿倾斜。实际上你必须创建一个布局,然后在该布局中添加两个文本视图......并使它们倾斜......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      相关资源
      最近更新 更多