0

我在 Eclipse 代码中显示以下规则。这条规则主要适用于所有类型的手机。但我的问题是,如果文本更大,就像在这种情况下,它会环绕在框内。有什么方法可以随着文本的增加自动使框变大?

TextView rule1


   //Add Rule 1
            rule1=new ImageView(this);      
            rule1.setBackgroundResource(R.drawable.rules);
            if(w<340)
             {
                layout_params=new RelativeLayout.LayoutParams(width(310), height(44));
                layout_params.leftMargin=width(5);
                layout_params.topMargin=height(30);         
             }else if(w<=500)
             {          
                layout_params=new RelativeLayout.LayoutParams(width(469), height(64));              
                layout_params.topMargin=height(40); 
                layout_params.leftMargin=width(6); 
             }else
             {
                layout_params=new RelativeLayout.LayoutParams(width(774), height(92));
                layout_params.leftMargin=width(13); 
                layout_params.topMargin=height(60);
             }  
            list_layout.addView(rule1, layout_params);


            icon6=new ImageView(this);      
         icon6.setBackgroundResource(R.drawable.leader);
            if(w<340)
             {
                layout_params=new RelativeLayout.LayoutParams(width(31), height(33));
                layout_params.leftMargin=width(12);
                layout_params.topMargin=height(39);         
             }else if(w<=500)
             {          
                layout_params=new RelativeLayout.LayoutParams(width(43), height(44));           
                layout_params.topMargin=height(52); 
                layout_params.leftMargin=width(18); 
             }else
             {
                layout_params=new RelativeLayout.LayoutParams(width(57), height(60));
                layout_params.leftMargin=width(30); 
                layout_params.topMargin=height(78);
             }  
            list_layout.addView(icon6, layout_params);

            rule1 = new TextView(this);
                rule1.setText("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
            rule1.setTextColor(Color.WHITE);
            rule1.setTextSize(18);      
            rule1.setGravity(Gravity.LEFT);
            typeFace=Typeface.createFromAsset(getAssets(), "fonts/HelveticaLTStd-Bold.otf");
            rule1.setTypeface(typeFace);
            if(w<340)
            {
                rule1.setTextSize(16);  
                layout_params=new RelativeLayout.LayoutParams(width(252), height(30));
                layout_params.leftMargin=width(40);
                layout_params.topMargin=height(39);         
            }else if(w<=500)
            {           
                layout_params=new RelativeLayout.LayoutParams(width(378), height(40));
                layout_params.leftMargin=width(72);
                layout_params.topMargin=height(52);             
            }else
            {
                layout_params=new RelativeLayout.LayoutParams(width(630), height(60));
                layout_params.leftMargin=width(120);
                layout_params.topMargin=height(78);                         
            }   
            rule1.setLayoutParams(layout_params);
            list_layout.addView(rule1);  
4

1 回答 1

0

我想你想要的就WRAP_CONTENT在你的身上

RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 150);

如果您愿意,也可以设置TextViewsingleLine

TextView.setSingleLine(true);
于 2012-06-05T00:16:53.960 回答