2017-08-09 77 views
-1

我正在创建一个TextView元素,以编程方式调整大小。但是,TextView有时对于文本来说太小,有时会切断更多的文本。以编程方式设置TextView大小使其小于wrap_content

有没有办法将TextView的最小高度设置为WRAP_CONTENT

编辑:最大尺寸可以大于WRAP_CONTENT。

+0

能否请您分享您的全部代码,如文本视图可能取决于其父容器(布局)上 –

+0

更多的调查表明,WRAP_CONTENT实际上会产生更大的问题,所以我想出了一种方法来破解TextView的最小尺寸作为更好的解决方法。 – Baracus

回答

1
TextView tv= new TextView(context); 
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
+0

更多的调查显示WRAP_CONTENT实际上会产生更大的问题,所以我想出了一种方法来破解TextView的最小尺寸作为更好的解决方法。感谢您的帮助。 – Baracus

0

请尝试像下面的代码;

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="MANAGER LOGIN" 
     android:textColor="#FFF"/> 
+0

更多调查显示WRAP_CONTENT实际上会产生更大的问题,所以我想出了一种方法来破解TextView的最小尺寸作为更好的解决方法。感谢您的帮助。 – Baracus

0

是的,你可以设置通过编程

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT); 
    textView.setLayoutParams(params); 
+1

更多调查显示WRAP_CONTENT实际上会产生更大的问题,所以我想出了一种方法来破解TextView的最小尺寸作为更好的解决方法。感谢您的帮助。 – Baracus

0
TextView tv = (TextView) findViewById(R.id.tv); 
LinearLayout llout = (LinearLayout) findViewById(R.id.llout); 

tv.measure(0, 0); 
int text = tv.getMeasuredWidth(); 
llout.measure(0,0); 
int lWidth = llout.getWidth(); 


if (text > (lWidth/3)) 
{ 
    tv.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f)); 
} 
else 
{ 
    tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
} 
+0

更多调查显示WRAP_CONTENT实际上会产生更大的问题,所以我想出了一种方法来破解TextView的最小尺寸作为更好的解决方法。感谢您的帮助。 – Baracus

相关问题