gpt4 book ai didi

android - 在 TextView 中以 45 度显示文本,具有自定义背景。

转载 作者:行者123 更新时间:2023-11-30 02:35:55 25 4
gpt4 key购买 nike

我在列表中有一个 textView,我想在其上以 45 度倾斜显示文本,并具有居中水平 + 居中垂直属性。到目前为止:

我的自定义 TextView 类:

   public class AngledTextView extends TextView  {  
public AngledTextView(Context context, AttributeSet attrs) {
super(context, attrs);

}

@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.rotate(45, this.getWidth()/2, this.getHeight()/2);
super.onDraw(canvas);
canvas.restore();
}
}

XML 代码:

<com.example.AngledTextView
android:id="@+id/textViewx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView2"
android:layout_alignParentRight="true"
android:layout_below="@+id/relativeSecondary"
android:background="@drawable/image_sticker" />

在类里面:

final AngledTextView tv_red = (AngledTextView)view.findViewById(R.id.textViewx);
tv_red.setText("Today");

结果:

enter image description here

期望:

enter image description here

最佳答案

我试图在我的项目中做同样的事情。为此,我使用了 android:rotationandroid:translationXandroid:translationY 属性。我添加了 android:paddingStartandroid:paddingEnd 来修复文本超出屏幕的错误。在对大小、翻译和填充进行了一些实验之后,我将这段代码用于短的两行文本:

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
android:id="@+id/textViewStatus"
android:layout_width="130dp"
android:layout_height="130dp"
android:gravity="bottom|center_horizontal"
android:maxLines="2"
android:paddingBottom="5dp"
android:paddingEnd="31dp"
android:paddingStart="31dp"
android:rotation="45"
android:text="@string/empty"
android:textColor="@android:color/white"
android:textSize="12sp"
android:translationX="65dp"
android:translationY="-65dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

关于android - 在 TextView 中以 45 度显示文本,具有自定义背景。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26563037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com