LayoutInflater似乎并没有与布局ID链接 [英] LayoutInflater doesn't seem to be linked with id in layout

查看:102
本文介绍了LayoutInflater似乎并没有与布局ID链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全与此LayoutInflater 丢失。我试图在code与地处偏远布局项目链接项目。我已经尝试了三种不同版本的充气创造。他们没有工作。但是,此版本似乎是最广泛使用的。这里是充气断章取义的一个片段:

 的setContentView(R.layout.browse);LayoutInflater李=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
最终的ImageButton editBrowseButton =(的ImageButton)li.inflate(R.layout.row,NULL).findViewById(R.id.imageButton1);
editBrowseButton.setAlpha(50);

这有点感觉就像我失去了一些东西。我需要返回的东西?该.setAlpha没有任何意义。我只是把它在测试inlater。很明显,它不改变透明度。如果我添加一个onClickListner,这是行不通的。但是,我没有得到一个例外。活动开始了罚款。下面是相关的XML code为row.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / linearLayout1
机器人:方向=横向
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:可聚焦=真
>
    <的TableRow
    机器人:ID =@ + ID / tableRow1
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =WRAP_CONTENT
    机器人:可聚焦=真
    >
    <的TextView
    机器人:ID =@ + ID / txtItem
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=项目
    机器人:可聚焦=真
   />
     < /&的TableRow GT;
    <的TableRow
    机器人:ID =@ + ID / tableRow2
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =WRAP_CONTENT
    机器人:可聚焦=假
    >
        <的ImageButton
        机器人:SRC =@绘制/ editbtn
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / imageButton1        >< / ImageButton的>    < /&的TableRow GT;
< / LinearLayout中>

EDIT_01

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

 的setContentView(R.layout.browse);    LayoutInflater李=(LayoutInflater)本
            .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李=(LayoutInflater)本
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看rowView =(查看)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);


解决方案

我不知道你有什么做的。据我所知LayoutInflater是从资源的XML文件创建视图。嗯,至少这是我用它。:D

一是通过tablerow应始终作为一个TableLayout的孩子 - 这样的XML看起来太可笑我 - 简单的LinearLayout将是一件好事。 (但是这不是侧面)

总之 - 我的问题与code看到的是,你夸大未连接视图(第二paramenter为null,比你把它挂)

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

 的setContentView(R.layout.browse);LayoutInflater李=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
连续的LinearLayout = li.inflate(R.layout.row,NULL);
findViewById(/ *元素的标识要的行被添加到* /)添加(行)。 //现在排连接
最终的ImageButton editBrowseButton =(的ImageButton)row.findViewById(R.id.imageButton1); //所以这是活的,可见等
editBrowseButton.setAlpha(50); //这应该不是工作

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);

解决方案

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:

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

这篇关于LayoutInflater似乎并没有与布局ID链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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