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);就无效了。。。大家知道原因么?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
中文显示有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);
}
######我试的你的都占全屏了,字够显示的时候是不会有省略号的######
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);
}
}