我正在尝试通过编程方式在RelativeLayout
的{{1}}上将孩子RelativeLayout
添加到父母Fragment
。但是,运行我的应用程序后,它没有通过onCreateView()
添加的子布局。
我创建了一个函数,用于返回以编程方式创建的RelativeLayout.addView()
。
下面是该函数的相应冗长代码,因为返回的RelativeLayout
的层次结构非常大:
RelativeLayout
在片段的private RelativeLayout createCards() {
RelativeLayout relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) (250 * scale + 0.5f), (int) (400 * scale + 0.5f));
layoutParams.setMargins(0, 0, (int) (15 * scale + 0.5f), 0);
relativeLayout.setLayoutParams(layoutParams);
relativeLayout.setBackgroundResource(R.drawable.thought_diary_list_blue);
relativeLayout.setClickable(true);
relativeLayout.setFocusable(true);
relativeLayout.setPadding((int) (8 * scale + 0.5f), (int) (90 * scale + 0.5f), (int) (8 * scale + 0.5f), (int) (80 * scale + 0.5f));
relativeLayout.setId(View.generateViewId());
RelativeLayout firstRow = new RelativeLayout(getContext());
RelativeLayout.LayoutParams firstRowLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams
.WRAP_CONTENT);
firstRowLayoutParams.setMargins((int) (10 * scale + 0.5f), 0, (int) (10 * scale + 0.5f), 0);
firstRow.setPadding(0, (int) (20 * scale + 0.5f), 0, 0);
firstRow.setLayoutParams(firstRowLayoutParams);
firstRow.setId(View.generateViewId());
LinearLayout linearLayout = new LinearLayout(getContext());
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams((int) (40 * scale + 0.5f), LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(linearLayoutParams);
TextView dateTextView = new TextView(getContext());
LinearLayout.LayoutParams dateTextViewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dateTextView.setTypeface(Typeface.create("avenir", Typeface.BOLD));
dateTextView.setText("16");
dateTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
dateTextView.setTextColor(Color.parseColor("#fff000"));
dateTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
dateTextView.setLayoutParams(dateTextViewLayoutParams);
TextView monthTextView = new TextView(getContext());
LinearLayout.LayoutParams monthTextViewLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
monthTextView.setTypeface(Typeface.create("avenir", Typeface.NORMAL));
monthTextView.setText("NOV");
monthTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
monthTextView.setTextColor(Color.parseColor("#fff000"));
monthTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
monthTextView.setLayoutParams(monthTextViewLayoutParams);
TextView yearTextView = new TextView(getContext());
LinearLayout.LayoutParams yearTextViewLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
yearTextView.setTypeface(Typeface.create("avenir", Typeface.NORMAL));
yearTextView.setText("1998");
yearTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
yearTextView.setTextColor(Color.parseColor("#fff000"));
yearTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
yearTextView.setLayoutParams(yearTextViewLayoutParams);
linearLayout.addView(dateTextView);
linearLayout.addView(monthTextView);
linearLayout.addView(yearTextView);
ImageView imageView = new ImageView(getContext());
RelativeLayout.LayoutParams imageViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
imageView.setLayoutParams(imageViewLayoutParams);
imageView.setBackgroundResource(R.drawable.icon_vertical_threedots);
firstRow.addView(linearLayout);
firstRow.addView(imageView);
relativeLayout.addView(firstRow);
RelativeLayout secondRow = new RelativeLayout(getContext());
RelativeLayout.LayoutParams secondRowLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
secondRowLayoutParams.addRule(RelativeLayout.BELOW, firstRow.getId());
secondRowLayoutParams.setMargins(0, (int) (20 * scale + 0.5f), 0, 0);
secondRow.setId(View.generateViewId());
secondRow.setLayoutParams(secondRowLayoutParams);
ImageView secondRowImageView = new ImageView(getContext());
RelativeLayout.LayoutParams secondRowImageViewLayoutParams = new RelativeLayout.LayoutParams((int) (250 * scale + 0.5f), (int) (150 * scale + 0.5f));
secondRowImageViewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
secondRowImageView.setBackgroundResource(R.drawable.td1);
secondRowImageView.setLayoutParams(secondRowLayoutParams);
secondRow.addView(secondRowImageView);
relativeLayout.addView(secondRow);
RelativeLayout thirdRow = new RelativeLayout(getContext());
RelativeLayout.LayoutParams thirdRowLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
thirdRowLayoutParams.addRule(RelativeLayout.BELOW, secondRow.getId());
thirdRowLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
thirdRowLayoutParams.setMargins((int) (10 * scale + 0.5f), 0, (int) (10 * scale + 0.5f), (int) (30 * scale + 0.5f));
thirdRow.setGravity(Gravity.BOTTOM);
thirdRow.setLayoutParams(thirdRowLayoutParams);
TextView thirdRowTextView = new TextView(getContext());
RelativeLayout.LayoutParams thirdRowTextViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams
.WRAP_CONTENT);
thirdRowTextViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
thirdRowTextView.setTypeface(Typeface.create("avenir", Typeface.BOLD));
thirdRowTextView.setText("12:00AM");
thirdRowTextView.setTextColor(Color.parseColor("#fff000"));
thirdRowTextView.setLayoutParams(thirdRowTextViewLayoutParams);
thirdRow.addView(thirdRowTextView);
relativeLayout.addView(thirdRow);
return relativeLayout;
}
上调用此函数,如下所示:
onCreateView
我承认,我在Android编程方面有点新。
预期结果应该是这样的视图:
更新:
在此处添加我的XML布局:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_diary, container, false);
RelativeLayout writeDiary = view.findViewById(R.id.writeDiary);
writeDiary.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent i= new Intent(getActivity(),WriteDiary.class);
startActivity(i);
}
});
ImageView imageView= view.findViewById(R.id.listIcon);
imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent i= new Intent(getActivity(),ListThoughts.class);
startActivity(i);
}
});
scale = getContext().getResources().getDisplayMetrics().density;
diary_mainLinearLayout = getActivity().findViewById(R.id.diary_mainLinearLayout);
if (diary_mainLinearLayout != null) {
diary_mainLinearLayout.addView(createCards());
}
return view;
}
更新2:
我已经设法使其在<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#f4f2fb"
android:gravity="center"
android:paddingTop="15dp"
tools:context=".modules.diary.DiaryFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="40dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/listIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginRight="30dp"
android:layout_marginBottom="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:src="@drawable/ic_list_black_24dp" />
</RelativeLayout>
<ScrollView
android:id="@+id/horScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/diary_mainLinearLayout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:fontFamily="@font/avenir_bold"
android:text="My Thoughts"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/writeDiary"
android:layout_width="250dp"
android:layout_height="400dp"
android:layout_marginRight="15dp"
android:background="@drawable/thought_diary_list"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:paddingStart="8dp"
android:paddingTop="90dp"
android:paddingEnd="8dp"
android:paddingBottom="80dp">
<TextView
android:layout_width="match_parent"
android:layout_height="215dp"
android:drawableTop="@drawable/td"
android:fontFamily="@font/avenir_regular"
android:gravity="center"
android:text="SHARE YOUR THOUGHTS"
android:textColor="#fff"
android:textSize="15sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/writeDiary2"
android:layout_width="250dp"
android:layout_height="400dp"
android:layout_marginEnd="15dp"
android:background="@drawable/thought_diary_list_blue"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:paddingStart="8dp"
android:paddingEnd="8dp">
<RelativeLayout
android:id="@+id/firstRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:orientation="horizontal"
android:paddingTop="20dp">
<LinearLayout
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/avenir_bold"
android:text="@string/date"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="22sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/avenir_regular"
android:text="@string/month"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="15sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/avenir_regular"
android:text="@string/year"
android:textAlignment="center"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:src="@drawable/icon_vertical_threedots" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/secondRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstRow"
android:layout_marginTop="20dp">
<ImageView
android:layout_width="250dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:src="@drawable/td1" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/thirdRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/secondRow"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="30dp"
android:gravity="bottom">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:fontFamily="@font/avenir_bold"
android:text="@string/time"
android:textColor="@color/white" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/writeDiary3"
android:layout_width="250dp"
android:layout_height="400dp"
android:layout_marginEnd="15dp"
android:background="@drawable/thought_diary_list_yellow"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:paddingStart="8dp"
android:paddingTop="90dp"
android:paddingEnd="8dp"
android:paddingBottom="80dp">
<TextView
android:layout_width="match_parent"
android:layout_height="215dp"
android:drawableTop="@drawable/td"
android:fontFamily="@font/avenir_regular"
android:gravity="center"
android:text="SHARE YOUR THOUGHTS"
android:textColor="#fff"
android:textSize="15sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/writeDiary4"
android:layout_width="250dp"
android:layout_height="400dp"
android:layout_marginRight="15dp"
android:background="@drawable/thought_diary_list"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:paddingStart="8dp"
android:paddingTop="90dp"
android:paddingEnd="8dp"
android:paddingBottom="80dp">
<TextView
android:layout_width="match_parent"
android:layout_height="215dp"
android:drawableTop="@drawable/td"
android:fontFamily="@font/avenir_regular"
android:gravity="center"
android:text="SHARE YOUR THOUGHTS"
android:textColor="#fff"
android:textSize="15sp" />
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/addIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="30dp"
android:background="@drawable/ovalpurple"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:src="@drawable/icon_addition" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
方法上执行addView()
的工作。干杯