2011-09-12 108 views
1

我在android.i新的开发者已经创建了该视图类视图类我想applay字符串数组值的TextView我的视图类如下:如何从视图类获取视图?

class MyView extends View 
{ 
    Context con; 
String messages[]=new String[]{"hello","hai","how are you","where are you"}; 
public MyView(Context context) { 
    super(context); 
    con=context; 

     for(int i=0;i<3;i++) 
    { 

     Log.v("messages", "messages"+messages[i]); 
    } 


    public View getView(int position, View view, ViewGroup vg) 
{ 
    if (view == null) 
    { 
     // context would be a variable set via the constructor and given 
     // by our owner (most likely a ListActivity) 
     LayoutInflater inflater = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.view, null); 
    } 

    // Here you would get the sub-views of your layout and fill them 
    TextView msg = (TextView) view.findViewById(R.id.txtmessage); 
    msg.setText(messages[position]); 
    return view; 
} 

}

从上面

类我想从上面的代码查看MyView的类的视图的活性类

 public class ViewActivity extends Activity { 



TableRow tbr; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view); 

    tbr=(TableRow)findViewById(R.id.tableRow1); 


    MyView mv=new MyView(this); 
    tbr.addView(mv); 

} 

}

我我无法查看MyView的类

的我怎样才能得到MyView的类

回答

2

的需要将设置的LayoutParams到您的视图的视图。现在你的观点没有大小。

你需要像这样

MyView mv=new MyView(this); 
mv.setLayoutParams(new LayoutParams(new LinearLayout.LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT))); 
tbr.addView(mv);