#android
#Android
Вопрос:
Приведен следующий пример макета с пользовательским представлением «RowWrapperView»:
(row_item.xml )
<?xml version="1.0" encoding="utf-8"?>
<com.example.android.RowWrapperView
xmlns:android="http://schemas.android.com/apk/res/android"
... >
<Button android:id="@ id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="@ id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="@ id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</com.example.android.RowWrapperView>
Представление имеет завышенный макет (view_row_wrapper.xml ) с линейным описанием с идентификатором «R.id.container»:
public class RowWrapperView extends RelativeLayout {
public RowWrapperView(Context context) {
super(context);
init();
}
public RowWrapperView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RowWrapperView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public LinearLayout container;
private void init() {
inflate(getContext(), R.layout.view_row_wrapper, this);
container = (LinearLayout) findViewById(R.id.container);
}
}
(view_row_wrapper.xml )
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
... >
<LinearLayout
android:id="@ id/something"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button android:id="@ id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
...
</LinearLayout>
<LinearLayout
android:id="@ id/container"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- Trying to get the content of the view here -->
</LinearLayout>
<LinearLayout
android:id="@ id/something_else"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
...
<TextView
android:id="@ id/message_sender"
tools:text="Jon Snow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Что я пытаюсь сделать, так это получить содержимое «RowWrapperView» (из row_item.xml ) в R.id.контейнере.
Я что-то упустил?
Ответ №1:
Inflater имеет возвращаемый тип — view, это представление, которое вы должны добавить в свой макет, затем выполнить поиск, попробуйте это, не добавляя view в layout
private void init() {
View view = inflate(getContext(), R.layout.view_row_wrapper, this);
container = (LinearLayout) view.findViewById(R.id.container);
}
или это с добавлением в макет
private void init() {
View view = inflate(getContext(), R.layout.view_row_wrapper, this);
addView(view);
container = (LinearLayout) findViewById(R.id.container);
}
Комментарии:
1. Это приведет к правильному раздуванию макета R.layout.view_row_wrapper в пользовательском представлении. Но как я могу получить содержимое тега RowWrapperView (в row_item.xml ) в R.id.контейнере?
Ответ №2:
Я думаю, вы могли бы сделать это:
for (int i=0 ; i<getChildCount() ; i ) {
container.add(getChildAt(i));
}
для добавления всех элементов в контейнер.
Комментарии:
1. Вы имеете в виду в init() ?
2. Да, потому что таким образом вы устанавливаете элементы в контейнер вместо текущего представления.