gpt4 book ai didi

java - 将动态 View 添加到RelativeLayout

转载 作者:行者123 更新时间:2023-12-01 13:07:29 25 4
gpt4 key购买 nike

我正在尝试按照以下方法制作一个日历应用程序: How can I create a weekly calendar view for an Android Honeycomb application?

但是我需要向相应的RelativeLayout添加一个 View ,但是每次我尝试运行时它崩溃的应用程序。

我的片段.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 的 View 必须在当天动态添加。

最佳答案

必须改变:

        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;
}

致:

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;
}

关于java - 将动态 View 添加到RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23162098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com