2

我完全迷失了这个 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);
4

2 回答 2

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
于 2011-12-13T22:50:30.933 回答
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); 
于 2011-06-30T22:20:26.930 回答