代码中新建TextView

TextView.setGravity(Gravity.CENTER);//居中
TextView.setGravity(Gravity.CENTER_HORIZONTAL);//水平居中
TextView.setGravity(Gravity.CENTER_VERTICAL);//垂直居中
TextView.setGravity(Gravity.LEFT);//靠左
TextView.setGravity(Gravity.RIGHT);//靠右

 TextView textView = new TextView(this);//新建控件
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//设置控件的宽高        
layoutParams.setMargins(5, 5, 5, 5);//设置控件与上下左右的距离
textView.setGravity(Gravity.LEFT);
textView.setBackgroundResource(android.R.color.white);//设置背景色 
textView.setTextColor(getResources().getColor(android.R.color.black));//控件字体颜色
textView.setLayoutParams(layoutParams);//上面设置控件的高宽后就落实
textView.setText(message);//控件内容


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 TextView 字体描边的方法有多种,以下是一种常见的方法: 1. 在 res/values 文件夹下新建一个名为 attrs.xml 的文件,添加以下代码: ``` <resources> <attr name="strokeWidth" format="dimension"/> <attr name="strokeColor" format="color"/> </resources> ``` 2. 在 TextView 标签添加以下属性: ``` <com.example.myapp.MyTextView android:id="@+id/myTextView" android:text="Hello World!" android:textSize="24sp" app:strokeWidth="2dp" app:strokeColor="#000000" android:textColor="#ffffff"/> ``` 其,MyTextView 是自定义的 TextView 类,继承自 android.widget.TextView。 3. 在 MyTextView重写 onDraw 方法,实现字体描边的效果: ``` public class MyTextView extends TextView { private float strokeWidth; private int strokeColor; public MyTextView(Context context) { super(context); } public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); strokeWidth = a.getDimension(R.styleable.MyTextView_strokeWidth, 0); strokeColor = a.getColor(R.styleable.MyTextView_strokeColor, Color.BLACK); a.recycle(); } @Override protected void onDraw(Canvas canvas) { TextPaint paint = getPaint(); // 设置描边效果 paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(strokeWidth); paint.setColor(strokeColor); // 画出描边效果 super.onDraw(canvas); // 恢复原有的颜色和样式 paint.setStyle(Paint.Style.FILL); paint.setColor(getCurrentTextColor()); super.onDraw(canvas); } } ``` 注意,在重写 onDraw 方法时,需要先设置描边效果,再调用父类的 onDraw 方法画出描边效果,最后恢复原有的颜色和样式再次调用父类的 onDraw 方法,以保证正常显示字体。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值