我完全失去了这个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);
的TableRow的TableRow =(的TableRow)li.inflate( R.layout.tableRow2,空) ; ImageButton editBrowseButton =(ImageButton)tableRow.findViewById(R.id.imageButton1); – Blundell
构造函数不喜欢“li.inflate(** R.layout.tableRow2 **,null)tableRow2不是布局。如果将同一行更改为”R.id.tableRow2“,则该活动将失败另外,注意到顶部的linearlayout标签在上面被切掉了tableRow2 tablerow放置在linearLayout的内部,这是否重要? – atomSmasher
如果您要使用layoutInflator.inflate(resource,null),请确保您知道潜在的副作用:http://www.doubleencore.com/2013/05/layout-inflation-as-intended/ – RTF