Я пытаюсь создать составную группу просмотра после накачивания группы из файла 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>