开发者社区> 问答> 正文

textView.setEllipsize(TruncateAt.END);无效:配置报错 

LinearLayout	nameLinearLayout = new LinearLayout(this);
    	nameLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
    	
    	TextView nameTextView = new TextView(this);
    	nameTextView.setSingleLine();
    	nameTextView.setEllipsize(TruncateAt.END);
    	nameTextView.setId(10);
    	nameTextView.setLayoutParams(new LinearLayout.LayoutParams(350, LinearLayout.LayoutParams.WRAP_CONTENT));
    	nameTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    	nameLinearLayout.addView(nameTextView);
		
		setContentView(mPhoneAndNameLinearLayout);
		
		TextView h = (TextView) findViewById(10);
		h.setText("为此恐怕的哦所佛第三方贫困vldflsd'fdsfvv劳动力大盘思路");




上面代码中在Activity里面创建控件 设置 nameTextView.setEllipsize(TruncateAt.END);
 文字过长会在后面显示省略号,
可是在 一个 继承 ViewGroup的类里面创建设置
 nameTextView.setEllipsize(TruncateAt.END);就无效了。。。大家知道原因么?

展开
收起
kun坤 2020-06-04 10:23:45 964 0
1 条回答
写回答
取消 提交回答
  • 中文显示有bug。参考博文《 textview多行ellipsize="end"不显示省略号的解决方法 》######谢谢,我的问题查到原因了,失误设置了之后又在另外一个地方设置了Ellipsize######public ImageViewText(Context context, int screenWidth) {

    super(context);

    imageView = new ImageView(context);

    imageView.setId(1);

    imageView.setLayoutParams(getSameViewLayout(context, screenWidth));

    addView(imageView);

    怎么解决··

    textView = new TextView(context);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(

    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    // params.topMargin = DensityUtil.dip2px(context, 10);

    params.addRule(RelativeLayout.BELOW, 1);

    params.addRule(RelativeLayout.ALIGN_LEFT, 1);

    params.addRule(RelativeLayout.ALIGN_RIGHT, 1);

    textView.setSingleLine(true);

    textView.setEllipsize(TruncateAt.END);

    textView.setLines(1);

    textView.setGravity(Gravity.CENTER);

    textView.setLayoutParams(params);

    addView(textView);

    }

    public ImageView getImageView() {

    return imageView;

    }

    public void setImageView(ImageView imageView) {

    this.imageView = imageView;

    }

    public TextView getTextView() {

    return textView;

    }

    public void setTextView(TextView textView) {

    this.textView = textView;

    }

    public void setText(String text){

    textView.setText(text);

    }

    public ImageViewText(Context context, AttributeSet attrs) {

    super(context, attrs);

    // TODO Auto-generated constructor stub

    }######public ImageViewText(Context context, int screenWidth) {

    super(context);

    imageView = new ImageView(context);

    imageView.setId(1);

    imageView.setLayoutParams(getSameViewLayout(context, screenWidth));

    addView(imageView);

    textView = new TextView(context);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(

    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    // params.topMargin = DensityUtil.dip2px(context, 10);

    params.addRule(RelativeLayout.BELOW, 1);

    params.addRule(RelativeLayout.ALIGN_LEFT, 1);

    params.addRule(RelativeLayout.ALIGN_RIGHT, 1);

    textView.setSingleLine(true);

    textView.setEllipsize(TruncateAt.END);

    textView.setLines(1);

    textView.setGravity(Gravity.CENTER);

    textView.setLayoutParams(params);

    addView(textView);

    }

    ######我试的你的都占全屏了,字够显示的时候是不会有省略号的######

    引用来自“ahere”的评论

    public ImageViewText(Context context, int screenWidth) {

    super(context);

    imageView = new ImageView(context);

    imageView.setId(1);

    imageView.setLayoutParams(getSameViewLayout(context, screenWidth));

    addView(imageView);

    textView = new TextView(context);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(

    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    // params.topMargin = DensityUtil.dip2px(context, 10);

    params.addRule(RelativeLayout.BELOW, 1);

    params.addRule(RelativeLayout.ALIGN_LEFT, 1);

    params.addRule(RelativeLayout.ALIGN_RIGHT, 1);

    textView.setSingleLine(true);

    textView.setEllipsize(TruncateAt.END);

    textView.setLines(1);

    textView.setGravity(Gravity.CENTER);

    textView.setLayoutParams(params);

    addView(textView);

    }

    你试一下,我这边可以
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		TextView textView = new TextView(this); 
    		RelativeLayout layout = new RelativeLayout(this);
    		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    				350, 50);
    		
    		//params.topMargin = DensityUtil.dip2px(context, 10);
    		params.addRule(RelativeLayout.BELOW, 1);
    		params.addRule(RelativeLayout.ALIGN_LEFT, 1);
    		params.addRule(RelativeLayout.ALIGN_RIGHT, 1);
    		textView.setBackgroundColor(Color.parseColor("#43d5bf"));
    		//textView.setLayoutParams(new LinearLayout.LayoutParams(350, LinearLayout.LayoutParams.WRAP_CONTENT));
    		textView.setSingleLine(true);
    		textView.setWidth(100);
    		textView.setEllipsize(TruncateAt.END);
    		textView.setText("dfsssssssssssssssssssssssssssssssssssssssss看到所佛");
    		textView.setLines(1);
    		textView.setGravity(Gravity.CENTER);
    		textView.setLayoutParams(params);
    		layout.addView(textView);
    		setContentView(layout);
    	}
    }
    2020-06-04 11:20:21
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载