LayoutInflater, похоже, не связан с id в макете - PullRequest
2 голосов
/ 01 июля 2011

Я полностью потерян с этим 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, это не сработает.Однако я не получаю исключения.Деятельность начинается нормально.Вот соответствующий XML-код для 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

Новый подход опробован и не удался.Те же результаты.Ничего не происходит.

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

Ответы [ 2 ]

0 голосов
/ 14 декабря 2011

Я не уверен, что ты пытаешься сделать.AFAIK LayoutInflater предназначен для «создания представлений» из XML-файлов ресурсов.Ну, по крайней мере, для этого я и использую: D.

Во-первых, «TableRow всегда должен использоваться как дочерний элемент TableLayout» - так что xml выглядит и мне смешно - простой LinearLayout был бы хорош.(но это не на стороне)

Во всяком случае - проблема, которую я вижу с кодом, состоит в том, что раздуваемое вами представление не прикреплено (2-й параметр имеет значение null, а вы оставляете его висящим).

Если вы хотите продублировать запись - создайте ее 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
0 голосов
/ 01 июля 2011

Теперь я вижу весь ваш вопрос: -)

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