Paramétrer width à wrap_content pour TextView via le code

Quelqu’un peut-il m’aider à définir la largeur de TextView à wrap_content via du code et non à partir de XML?

Je crée dynamicment un TextView dans le code, alors comment définir sa largeur à wrap_content par le code?

 TextView pf = new TextView(context); pf.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

ou

 parentView.addView(pf, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

Il y a une autre façon d’obtenir le même résultat. Si vous ne devez définir qu’un seul paramètre, par exemple “height”:

 TextView textView = (TextView)findViewById(R.id.text_view); ViewGroup.LayoutParams params = textView.getLayoutParams(); params.height = ViewGroup.LayoutParams.WRAP_CONTENT; textView.setLayoutParams(params); 

La solution la plus simple pour modifier la largeur de WRAP_CONTENT en WRAP_CONTENT

 textView.getLayoutParams().width = LinearLayout.LayoutParams.WRAP_CONTENT; // For change `TextView` width to `WRAP_CONTENT` //textView.getLayoutParams().width = 200; // For change `TextView` width to 200 

J’espère que cette aide

Je pense que ce code répond à votre question

 RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.desc1.getLayoutParams(); params.height = RelativeLayout.LayoutParams.WRAP_CONTENT; holder.desc1.setLayoutParams(params);