LayoutInflater 似乎没有与布局中的 id 链接
我完全迷失了这个 LayoutInflater。我正在尝试将代码中的项目与位于远程布局中的项目链接起来。我尝试了三种不同版本的充气机创作。它们都不起作用。然而,这个版本似乎是使用最广泛的。这是充气器乱码的片段:
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);
I am completely lost with this LayoutInflater. I am trying to link items in code with items located in a remote layout. I have tried three different versions of the inflater creation. None of them work. However, this version seems to be the most widely used. Here is a snippet of the inflater garble:
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);
This feels kinda like I missing something. Do I need to return something? The .setAlpha has no meaning. I just put it in to test the inlater. Obviously, it doesn't change the transparency. And if I add an onClickListner, it doesn't work. However, I don't get an exception. The activity starts up fine. Here is the relevant XML code for row.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
New approach tried and failed. Same results. Nothing happens.
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在我可以看到你的整个问题了:-)
Now that I can see your whole question :-)
我不确定你想做什么。 AFAIK LayoutInflater 用于从资源 xml 文件“创建视图”。好吧,至少我就是这么用它的:D。
首先,“TableRow 应该始终用作 TableLayout 的子级” - 所以 xml 看起来也很有趣 - 简单的 LinearLayout 会很好。 (但这不在侧面)
无论如何 - 我在代码中看到的问题是您膨胀的视图未附加(第二个参数为空,并且您将其挂起)。
如果您想复制该条目 - 创建它 n 次,您应该这样做:
I'm not sure what are you trying to do. AFAIK LayoutInflater is for "creating views" from resource xml files. Well at least that's what I use it for :D.
First "A TableRow should always be used as a child of a TableLayout" - so the xml looks funny too me - simple LinearLayout would be good. (but that's not on the side)
Anyhow - problem I see with the code is that the view that you inflate is not attached (2nd paramenter is null, and than you leave it hanging).
If you want to duplicate the entry - create it n-times you should do it like this: