我有activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="blahblah.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
<!--here i want to include another layout-->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_content"
android:orientation="horizontal"></LinearLayout>
<!--
<include layout="@layout/content_main" />
-->
</android.support.design.widget.CoordinatorLayout>
在线性布局main_content中我想包含kalendar_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView tv;
LinearLayout mainContent;
KalendarView kv;
Context context;
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater=getLayoutInflater();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
mainContent=(LinearLayout) findViewById(R.id.main_content);
kv = new KalendarView(context, inflater);//this view i want to include in main layout
mainContent.addView(kv);//trying to include
setSupportActionBar(toolbar);
tv= (TextView) findViewById(R.id.textView11);
}
(...)
和KalendarView.java
public class KalendarView extends LinearLayout {
// LayoutInflater inflater;
public KalendarView(Context context, LayoutInflater inflater){
super(context);
// inflater = (LayoutInflater)context.getSystemService
// (Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.kalendar_view, null);//trying to inflate
}
//(...) some methods to programically change layout
}
这个应用程序崩溃,我不知道为什么。 我的目标是拥有多个不同的KalendarView类实例,以便我可以以编程方式显示其中一个。
有人可以解释一下我的代码有什么问题,以及如何正确地将一个布局包含在另一个布局中?
答案 0 :(得分:0)
你的KalendarView不应该有一个充气机作为参数,绝对不应该试图给自己充气。 您的Kalendar视图应如下所示:
public class KalendarView extends LinearLayout {
public KalendarView(Context context){
super(context);
}
//(...) some methods to programically change layout
}
这足以能够实例化视图。在您的主要活动中,您忘记了即时连接主要内容。看起来应该是这样的:
public class MainActivity extends AppCompatActivity {
TextView tv;
LinearLayout mainContent;
KalendarView kv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainContent = (LinearLayout) findViewById(R.id.main_content);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
kv = new KalendarView(MainActivity.this);
mainContent.addView(kv);
setSupportActionBar(toolbar);
tv= (TextView) findViewById(R.id.textView11);
}
(...)
答案 1 :(得分:0)
问题是,您需要将其附加到root,就像这样
private void initView() {
LayoutInflater.from(getContext()).inflate(R.layout.kalendar_view, this, true);
}
所以你的课应该是这样的:
public class KalendarView extends LinearLayout {
public KalendarView(Context context) {
this(context, null);
}
public KalendarView(Context context,
@Nullable AttributeSet attrs) {
super(context, attrs);
initView();
}
public KalendarView(Context context,
@Nullable AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
LayoutInflater.from(getContext()).inflate(R.layout.kalendar_view, this, true);
}
}