我已经采用了Calroid的示例代码并在我的项目中实现了相同的代码。 Follwing是代码。但我得到一个例外您必须在每个案例中提供layout_width属性。我在我的项目中已经尝试了几种方法,但仍然遇到同样的错误。
活动代码
public class CustomActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_layout);
// Set a toolbar to replace the action bar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frame_container);
CaldroidFragment caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
caldroidFragment.setArguments(args);
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.calendar1, caldroidFragment);
t.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
// Create the interstitial.
}}
XML代码custom_layout.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/cordinationLayout"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/app_bar"/>
<FrameLayout
android:id="@+id/calendar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</FrameLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
我收到以下异常
Process: com.eduims.portal, PID: 2511
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eduims.portal/com.eduims.portal.StudentFragmentActivity}: java.lang.RuntimeException: Binary XML file line #13: You must supply a layout_width attribute.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2192)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2241)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5049)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Binary XML file line #13: You must supply a layout_width attribute.
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:492)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5948)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6117)
at android.widget.RelativeLayout$LayoutParams.<init>(RelativeLayout.java:1225)
at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1062)
at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:82)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at com.roomorama.caldroid.CaldroidFragment.onCreateView(CaldroidFragment.java:1241)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:511)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5259)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2165)
答案 0 :(得分:0)
查看app_bar
文件夹中的layout
文件。我认为那里缺少layout_width attribute
。将其设置为android:layout_width="match_parent"
或android:layout_width="wrap_content"
(取决于哪些匹配更好)。
答案 1 :(得分:0)
尝试将宽度和高度设置为包含的布局。
<include
layout="@layout/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>