Как заполнить составную группу просмотра, выходящую из LinearLayout?
Я пытаюсь создать составную группу просмотра после раздувания группы из файла XML.
Группа просмотра состоит из: Корень LinearLayout, 2 дочерних LinearLayouts. Я могу правильно видеть макет в редакторе макетов; однако, когда я пытаюсь добавить представление (скажем, кнопку) из редактора, оно не отображается, и приложение немедленно принудительно закрывается. Мне сказали, что мне может понадобиться переопределить метод onLayout, чтобы правильно прорисовать компоненты представления, но я довольно запутался.
Мои занятия:
public class FocusBox extends LinearLayout {
private LinearLayout rootLayout,
contentLayout,
topLayout;
public FocusBox(Context context)
{
super(context, null);
}
public FocusBox(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.focus_box, this);
rootLayout = (LinearLayout)findViewById(R.id.focusBoxParent);
contentLayout = (LinearLayout)findViewById(R.id.focusBottom);
topLayout = (LinearLayout)findViewById(R.id.focusTop);
}
}
И XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:id="@+id/focusBoxParent"
android:orientation="vertical">
<LinearLayout
android:background="@drawable/gradients"
android:layout_weight=".1"
android:gravity="center"
android:id="@+id/focusTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="TextView"
android:id="@+id/focusTitle"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_width="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight=".9"
android:id="@+id/focusBottom"
android:background="@drawable/gradient2"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
1 ответ
Как правило, если вы раздуваете макет из допустимого XML, вы не должны получить ошибку. Вы должны сделать чистую сборку и заново развернуть приложение. Также проверьте, используете ли вы правильный класс в операторе импорта в других классах (вы можете использовать FocusBox
из какой-то сторонней библиотеки вместо той, которую вы сделали)