Android/レイアウト/カスタムレイアウトファイルをjavaで取得
提供: 初心者エンジニアの簡易メモ
カスタムレイアウト呼び出し
CustomView itemLayout = (CustomView)inflater.inflate(R.layout.costum1, null);
res/layout/custom1.xml
<jp.co.hoge.hogeproject.CustomView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/bodyView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hoge"
/>
</jp.co.hoge.hogeproject.CustomView>
"android.view.InflateException: Binary XML file Error inflating class inflater.inflate("エラーが出る時
publicのconstructを4つすべて作成すると治る。
public class CustomView extends LinearLayout {
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}
