我正试图在这个tut之后制作一个calander应用程序: How can I create a weekly calendar view for an Android Honeycomb application?
但我需要添加一个视图到相应的RelativeLayout但每次我尝试运行 应用程序崩溃了。
我的片段.java文件:
public class Calendar extends Activity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class FragmentCalendar extends Fragment {
public FragmentCalendar() {
RelativeLayout activeDay = (RelativeLayout)getActivity(). findViewById(R.id.sundayRelativeLayout);
View viw = new View(null);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,2);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
viw.setBackgroundColor(Color.BLUE);
viw.setLayoutParams(lay);
activeDay.addView(viw);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar,
container, false);
return rootView;
}
}
我的fragment_Calendar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.TJ.rijschool_app.Calendar"
android:orientation="vertical"
android:padding="0dp"
>
<LinearLayout
android:id="@+id/dayLabelsLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" >
<RelativeLayout
android:id="@+id/currentMonthRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="1" >
<TextView
android:id="@+id/currentMonthTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text=""
android:textSize="15dp"
android:textStyle="bold"
android:textColor="@color/medium_dark_gray" />
<TextView
android:id="@+id/currentYearTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text=""
android:textSize="13dp"
android:textStyle="bold"
android:textColor="@color/medium_dark_gray" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/sundayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="@+id/sundayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="@color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/sundayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="@string/sun"
android:textColor="@color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
<View
android:id="@+id/dayMarkerView"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:background="@color/strong_blue" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/mondayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="@+id/mondayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="@color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/mondayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:gravity="center"
android:text="@string/mon"
android:textColor="@color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/tuesdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="@+id/tuesdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="@color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tuesdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="@string/tue"
android:textColor="@color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/wednesdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="@+id/wednesdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="@color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/wednesdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="@string/wed"
android:textColor="@color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/thursdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="@+id/thursdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="@color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/thursdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="@string/thu"
android:textColor="@color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/fridayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="@+id/fridayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:text=""
android:textColor="@color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/fridayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="@string/fri"
android:textColor="@color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/saturdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="@+id/saturdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="@color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/saturdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="@string/sat"
android:textColor="@color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
不得不删除有关时间的部分,因为文件在这里发布很大。
正如您所看到的,必须在当天动态添加名为dayMarkerView的视图。
答案 0 :(得分:1)
不得不改变:
public FragmentCalendar() {
RelativeLayout activeDay = (RelativeLayout)getActivity(). findViewById(R.id.sundayRelativeLayout);
View viw = new View(null);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,2);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
viw.setBackgroundColor(Color.BLUE);
viw.setLayoutParams(lay);
activeDay.addView(viw);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar,
container, false);
return rootView;
}
TO:
public FragmentCalendar() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = new View(getActivity());
v = inflater.inflate(R.layout.fragment_calendar, container, false);
View pl = v.findViewById(R.id.thursdayHeaderRelativeLayout);
View Paper = new View(pl.getContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,2);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Paper.setLayoutParams(params);
Paper.setBackgroundColor(getActivity().getColor(R.color.strong_blue));
((RelativeLayout)pl).addView(Paper);
//Zet de datum op zondag
//View tv = v.findViewById(R.id.sundayDateTextView);
//((TextView)tv).setText("13");
return v;
}