开发者

LayoutInflater doesn't seem to be linked with id in layout

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


Now that I can see your whole question :-)

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜