Android的tablelayout麻烦,添加视图 [英] android tablelayout trouble, adding views

查看:181
本文介绍了Android的tablelayout麻烦,添加视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有查看领袖,我不知道是什么的第二个参数应该是麻烦

I am having trouble with View leader , I'm not sure what the second parameter is supposed to be

        TableLayout leaderTable = (TableLayout)findViewById(R.id.leaderTable);            

        TableRow tr = new TableRow(this);
        tr.setId(i);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        View leader = new View(UserView.this, null, R.id.leaderLayout);

        TextView number = (TextView)leader.findViewById(R.id.numberView);
        number.setText(String.valueOf(i+1));

        tr.addView(leader);

        leaderTable.addView(tr);

问题是,我的的TextView 为null尽管是领袖的子视图。

pretty困惑这个问题,这是我的XML

Pretty confused about this issue, and this is my XML

<TableLayout 
                 android:id="@+id/leaderTable"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center">

            </TableLayout>

我需要用XML做更多?我没有必要在它的tablerows是吧?

do I need to do more with the XML? I don't have need to include the tablerows in it do I?

R.id.leaderLayout 是它自己的XML文件,与该ID的LinearLayout中

R.id.leaderLayout is its own xml file, a linearlayout with that id

推荐答案

下面是开发商页面查看构造函数:

Here's the developer page for the View constructors:

<一个href=\"https://developer.android.com/reference/android/view/View.html#View%28android.content.Context%29\" rel=\"nofollow\">https://developer.android.com/reference/android/view/View.html#View%28android.content.Context%29

如果你想这个观点有对创造这一观点制定的某些属性或样式视图的第二和第三个参数被使用。

The second and third parameters for a View are used if you want this view to have certain attributes or styles set upon the creation of that view.

看起来你真正想要的变量领导者来充气。这将在XML定义一个布局并将其分配给动态创建的视图。你说你leaderLayout是一个LinearLayout中,所以它会是这个样子。

It looks like you actually want your variable leader to be inflated. This will take a layout defined in xml and assign it to dynamically created view. You said that your leaderLayout is a LinearLayout, so it would look something like this.

//Initialize the layout inflator, do this once and use it to inflate as many views as you want
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

//Assign your custom view to the variable leader
LinearLayout leader = (LinearLayout) inflator.inflate(R.layout.leaderLayout, tr); 

有关充气第一个参数是R.layout。 nameOfYou​​rXmlFile 的。第二个是一个ViewGroup,这将是你的充气查看父。一旦做到这一点,你可以使用 findViewById 上的领导者来帮助你在XML文件子视图,动态地添加更多的孩子,并将其添加为您的TableRow的孩子。

The first parameter for inflate is R.layout.nameOfYourXmlFile. The second is the ViewGroup that will be the parent of your inflated View. Once this is done, you can use findViewById on leader to get the child views in your Xml file, add more children dynamically, and add it as a child of your TableRow.

下面是LayoutInflator开发者页面,如果你很好奇其他用途的膨胀方法。

Here's the developer page for LayoutInflator, in case you're curious about other usages of the inflate method.

http://developer.android.com/reference/android/view/ LayoutInflater.html

这篇关于Android的tablelayout麻烦,添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆