梦天2015 2017-07-10 06:13 采纳率: 33.3%
浏览 1918
已结题

如何让textview内容居中,包裹内容,长=宽

这是我自定义的Textview
public class RectTextView extends android.support.v7.widget.AppCompatTextView {

public RectTextView(Context context) {
super(context);
}
public RectTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public RectTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
center();
}
private void center() {
int max = Math.max(getMeasuredHeight(), getMeasuredWidth());
setMeasuredDimension(max, max);
}
}
然后放到xml中设置gravity=center,长宽包裹内容,发现这样保障了长=宽,textview的内容却没有居中,求大神支招

  • 写回答

4条回答 默认 最新

  • 关注

    帮你试了一下,在布局文件中的RectTextView加上 android:gravity="center" 就行

    评论

报告相同问题?

悬赏问题

  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题