2011-06-30 48 views
2

我完全失去了这个LayoutInflater。我试图将代码中的项目与位于远程布局中的项目链接起来。我尝试过三种不同版本的inflater创作。他们都没有工作。但是,这个版本似乎是使用最广泛的。这里是一个充气器的代码片断:LayoutInflater似乎没有与布局中的id链接

setContentView(R.layout.browse); 

LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
final ImageButton editBrowseButton = (ImageButton) li.inflate(R.layout.row, null).findViewById(R.id.imageButton1); 
editBrowseButton.setAlpha(50); 

这感觉有点像我想念的东西。我需要退货吗? .setAlpha没有意义。我只是把它放进来测试插入器。显然,它不会改变透明度。如果我添加一个onClickListner,它不起作用。但是,我没有例外。活动开始正常。下面是row.xml相关的XML代码:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayout1" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:focusable="true" 
> 
    <TableRow 
    android:id="@+id/tableRow1" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:focusable="true" 
    > 
    <TextView 
    android:id= "@+id/txtItem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text = "Item" 
    android:focusable="true" 
    /> 
    </TableRow> 


    <TableRow 
    android:id="@+id/tableRow2" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:focusable="false" 
    > 
     <ImageButton 
     android:src="@drawable/editbtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageButton1" 

     ></ImageButton> 



    </TableRow> 
</LinearLayout> 

EDIT_01

新方法尝试和失败。相同的结果。什么都没发生。

setContentView(R.layout.browse); 

    LayoutInflater li = (LayoutInflater) this 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    ViewGroup rowView = (ViewGroup) li.inflate(R.layout.row, null); 
    LinearLayout rowLinLay = (LinearLayout) rowView 
      .findViewById(R.id.linearLayout1); 
    TableRow rowTableRow = (TableRow)rowLinLay.findViewById(R.id.tableRow2); 
    ImageButton editBrowseButton = (ImageButton) rowTableRow 
      .findViewById(R.id.imageButton1); 

EDIT_02

setContentView(R.layout.browse); 
    ExpandableListView browseView = (ExpandableListView)  findViewById(android.R.id.list); 

    LayoutInflater li = (LayoutInflater) this 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = (View) li.inflate(R.layout.row, null); 
    TableRow rowTable = (TableRow) rowView.findViewById(R.id.tableRow2); 
    ImageButton editBrowseButton = (ImageButton) rowTable 
      .findViewById(R.id.imageButton1); 
    editBrowseButton.setAlpha(100); 
+0

的TableRow的TableRow =(的TableRow)li.inflate( R.layout.tableRow2,空) ; ImageButton editBrowseButton =(ImageButton)tableRow.findViewById(R.id.imageButton1); – Blundell

+0

构造函数不喜欢“li.inflate(** R.layout.tableRow2 **,null)tableRow2不是布局。如果将同一行更改为”R.id.tableRow2“,则该活动将失败另外,注意到顶部的linearlayout标签在上面被切掉了tableRow2 tablerow放置在linearLayout的内部,这是否重要? – atomSmasher

+0

如果您要使用layoutInflator.inflate(resource,null),请确保您知道潜在的副作用:http://www.doubleencore.com/2013/05/layout-inflation-as-intended/ – RTF

回答

0

现在,我可以看到你的整个问题:-)

LinearLayout layout = (LinearLayout) li.inflate(R.layout.linearLayout1, null); 
TableRow tableRow = (TableRow) linearLayout1.findViewById(R.layout.tableRow2); 
ImageButton editBrowseButton = (ImageButton) tableRow.findViewById(R.id.imageButton1); 
+0

对不起,这不会让我通过LinearLayout1作为布局。布局的名称是行( – atomSmasher

+0

)是的所以你有没有尝试过id?li.inflate(R.id.linearLayout1,null);你能看到我在做什么,布局不应该叫Row,它应该是浏览(你用setContentView使用的xml文件) – Blundell

+0

是的,我尝试了所有可能的组合ption:*资源ID#0x7f050019类型#0x12无效*当我使用id代替布局时。真正的WIERD是如果我使用上面显示的** edit_02 **格式,mAlpha(在变量调试窗口中)从255更新到100。但是按钮的透明度不会改变。它几乎就像按钮所做的改变,它只是不知道它。 – atomSmasher

0

我不知道你有什么做的。 AFAIK LayoutInflater用于从资源xml文件“创建视图”。那么至少这是我用它的:D。

首先“TableRow应该总是用作TableLayout的子元素” - 所以xml看起来也很有趣 - 简单的LinearLayout会很好。 (但这不是在一边)

无论如何 - 我看到与代码的问题是,你膨胀的观点没有附加(第二个参数是空的,并且比你离开它悬挂)。

如果你要复制的条目 - 创建n次,你应该做这样的:

setContentView(R.layout.browse); 

LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
LinearLayout row = li.inflate(R.layout.row, null); 
findViewById(/*id of element you want the row to be added to*/).add(row); //now row is attached 
final ImageButton editBrowseButton = (ImageButton) row.findViewById(R.id.imageButton1); //so this is live, visible etc 
editBrowseButton.setAlpha(50); //this should work than